当前位置:网站首页>SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
2022-04-23 07:48:00 【Dake mountain man】
Sometimes you need to move forward or backward according to the value of a specified column N Check the relevant column values , Then use this SQL Custom scalar valued functions can get the relevant SQL sentence :
ALTER FUNCTION GetPreviousNextCodeSql
(
@tableName varchar(50),
@codeFieldName varchar(50),
@currentCode varchar(50),
@idKey varchar(50),
@step int
) --('B_ProcessList','process_code','OCV3','processid',-1)
RETURNS varchar(2000)
AS
BEGIN
DECLARE @result varchar(50), @sql varchar(2000)
set @result=''
-- To determine if there is idx Name , if there be , Use idx Sort , Otherwise use processid Sort
--if exists(select 1 from syscolumns where name='idx' and id=object_id(@tableName))
--Begin
set @sql = 'select t.'+ @codeFieldName +' from ('
set @sql = @sql +'select row_number() over (order by ' + @idKey +' asc) as p, ' + @codeFieldName +' from ' + @tableName
set @sql = @sql +') as t'
set @sql = @sql + ' where t.p =('
set @sql = @sql +' select top 1 m.p'
IF @step>0
set @sql = @sql +'+'
ELSE
set @sql = @sql
set @sql = @sql + convert(varchar,@step)
set @sql = @sql +' from ('
set @sql = @sql +' select row_number() over (order by ' + @idKey +' asc) as p, ' + @codeFieldName +' from ' + @tableName
set @sql = @sql +') as m where ' + @codeFieldName + '=''' + @currentCode +''')'
return @sql
--End
--return ''
END
Invocation time ( give an example ):
-- Check the second record above
select dbo.GetPreviousNextCodeSql('B_ProcessList','process_code','OCV3','processid',-2)
-- Check the second record
select dbo.GetPreviousNextCodeSql('B_Process_Flow','flow_code','GY002','id',+1)
The statement generated after the call is similar to the following :
select t.process_code from (select row_number() over (order by processid asc) as p, process_code from B_ProcessList) as t where t.p =( select m.p-2 from ( select row_number() over (order by processid asc) as p, process_code from B_ProcessList) as m where process_code='OCV3')
Key statement :
-- Check the previous process record
select t.process_code from (
select row_number() over (order by processid asc) as p, process_code from B_ProcessList
) as t
where t.p =(
select m.p-1 from (
select row_number() over (order by processid asc) as p, process_code from B_ProcessList
) as m where process_code='OCV3')
Of course, you can also use and similar statements to realize , But the flexibility is not as good as the above , Because you can only look up or down / The latter :
-- Check the next process record
select process_code from B_ProcessList where processid=
(select max(processid) from B_ProcessList where processid<
(select processid from B_ProcessList where process_code='OCV3'))
-- Check the previous process record
select process_code from B_ProcessList where processid=
(select min(processid) from B_ProcessList where processid>
(select processid from B_ProcessList where process_code='OCV3'))
OK.
版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378755.html
边栏推荐
- SVG中Path Data数据简化及文件夹所有文件批量导出为图片
- Thorough inquiry -- understanding and analysis of cocos2d source code
- c#读取INI文件和向ini文件写入数据
- Scrapy modifies the time in the statistics at the end of the crawler as the current system time
- Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
- js之排他思想及案例
- 读取修改resource文件夹下的json文件
- Double sided shader
- Date object (JS built-in object)
- js之DOM事件
猜你喜欢

SAP CR传输请求顺序、依赖检查

Mongodb starts warning information processing

Scrapy modifies the time in the statistics at the end of the crawler as the current system time

Date object (JS built-in object)

Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism

ABAP 实现发布RESTful服务供外部调用示例

js之DOM学习获取元素

SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)

Dropping Pixels for Adversarial Robustness

对js中argumens的简单理解
随机推荐
移动Web(字体图标、平面转换、颜色渐变)
C# 多个矩形围成的多边形标注位置的问题
平面定义-平面方程
SampleCameraFilter
【NLP笔记】CRF原理初探
基于NLP的软件安全研究(二)
Custom time format (yyyy-mm-dd HH: mm: SS week x)
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
Shapley Explanation Networks
BTREE, B + tree and hash index
移动端布局(3D转换、动画)
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
King glory - unity learning journey
Ogldev reading notes
学会使用搜索引擎
C# SmoothProgressBar自定义进度条控件
js之DOM学习三种创建元素的方式
js之预解析
MySQL8.0 安装/卸载 教程【Window10版】
简单理解==和equals,String为什么可以不用new