当前位置:网站首页>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
边栏推荐
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- The page displays the current time in real time
- C# 读取注册表
- C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
- Use of command line parameter passing library argparse
- Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
- Date object (JS built-in object)
- 双面显示的shader
- Hot change scheme and dynamic update strategy of mobile game
- 颜色转换公式大全及转换表格(31种)
猜你喜欢

SAP DEBUG调试FOR IN、REDUCE等复杂的语句

中间人环境mitmproxy搭建

Dropping Pixels for Adversarial Robustness

js之预解析

Ogldev reading notes

FSM finite state machine

MySQL8.0 安装/卸载 教程【Window10版】

Django uses MySQL database to solve error reporting

Simple random roll call lottery (written under JS)

Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
随机推荐
Daily question | fear dominated by reverse linked list
03Scanner类的使用(控制台输入)
Unity 获取一个文件依赖的资源
SAP PI/PO登录使用及基本功能简介
js之DOM学习三种创建元素的方式
移动端布局(3D转换、动画)
C reads the registry
Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
'NPM' is not an internal or external command, nor is it a runnable program or batch file
ABAP 实现发布RESTful服务供外部调用示例
MySQL storage engine
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
unity UGUI判断点击在UI上和3D物体上的解决方案
Install and configure Taobao image NPM (cnpm)
js之预解析
Date object (JS built-in object)
系统与软件安全研究(三)
C# SmoothProgressBar自定义进度条控件
Apache Hudi 如何加速传统的批处理模式?