当前位置:网站首页>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
边栏推荐
猜你喜欢

C# 多个矩形围成的多边形标注位置的问题

'NPM' is not an internal or external command, nor is it a runnable program or batch file

Dropping Pixels for Adversarial Robustness

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

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

防抖和节流

H5 local storage data sessionstorage, localstorage

ABAP 从CDS VIEW 发布OData Service示例

FSM finite state machine

对复杂字典Dictionary&lt;T1,T2&gt;排序问题
随机推荐
King glory - unity learning journey
自己封装unity的Debug函数
C# 文本文件的查找及替换(WinForm)
Django uses MySQL database to solve error reporting
颜色转换公式大全及转换表格(31种)
Unity ugui determines the solution of clicking on the UI and 3D objects
Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
js之作用域、作用域链、全局变量和局部变量
基于NLP的软件安全研究(一)
Quick sort
二叉树的深度
Event system (II) multicast events
js之DOM学习获取元素
简单理解==和equals,String为什么可以不用new
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Simple random roll call lottery (written under JS)
Install and configure Taobao image NPM (cnpm)
SAP PI/PO登录使用及基本功能简介
大学学习路线规划建议贴
平面定义-平面方程