当前位置:网站首页>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
边栏推荐
- 移动端布局(3D转换、动画)
- 简单理解==和equals,String为什么可以不用new
- Apache Hudi 如何加速传统的批处理模式?
- SAP CR传输请求顺序、依赖检查
- The page displays the current time in real time
- canvas学习第一篇
- Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
- Use of typescript dictionary
- NodeJS(一) 事件驱动编程
- 03Scanner类的使用(控制台输入)
猜你喜欢
中间人环境mitmproxy搭建
Dropping Pixels for Adversarial Robustness
ABAP 实现发布RESTful服务供外部调用示例
Redis connection error err auth < password > called without any password configured for the default user
Ogldev reading notes
C# 多个矩形围成的多边形标注位置的问题
js之DOM事件
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
FSM finite state machine
Window10版MySQL设置远程访问权限后不起效果
随机推荐
学会使用搜索引擎
简单理解==和equals,String为什么可以不用new
Use of typescript dictionary
promise all的实现
js之什么是事件?事件三要素以及操作元素
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
ABAP 7.4 SQL Window Expression
js之DOM事件
TimelineWindow
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
js之预解析
State synchronization and frame synchronization
C#操作注册表全攻略
js之自定义属性以及H5中如何判断自定义属性
中间人环境mitmproxy搭建
NodeJS(一) 事件驱动编程
【NLP笔记】CRF原理初探
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
C# SmoothProgressBar自定义进度条控件