当前位置:网站首页>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
边栏推荐
- King glory - unity learning journey
- C# 多个矩形围成的多边形标注位置的问题
- Encapsulate the debug function of unity
- SAP PI/PO功能运行状态监控检查
- 保研准备经验贴——18届(2021年)中南计科推免到浙大工院
- Unity screen adaptation
- Nodejs (I) event driven programming
- Redis connection error err auth < password > called without any password configured for the default user
- Protobuf 使用
- Moment. Format of format method function in JS
猜你喜欢

SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)

Window10版MySQL设置远程访问权限后不起效果

基于NLP的软件安全研究(二)

SAP PI/PO功能运行状态监控检查

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)

SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。

canvas学习第一篇

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

命令行参数传递库argparse的使用

ABAP 7.4 SQL Window Expression
随机推荐
js之函数的两种声明方式
异步的学习
Protobuf 使用
对js中argumens的简单理解
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
'NPM' is not an internal or external command, nor is it a runnable program or batch file
MySQL storage engine
Nodejs (II) read files synchronously and asynchronously
12. Constraints
取得所有点列表中的最大值GetMaxPoint
Event system (II) multicast events
promise all的实现
03Scanner类的使用(控制台输入)
BTREE, B + tree and hash index
学会使用搜索引擎
SampleCameraFilter
Ogldev reading notes
Shapley Explanation Networks
Unity 获取一个文件依赖的资源
Mobile game performance optimization