是否可以使用逗号分隔变量和TSQL IN运算符过滤记录列表?

编程入门 行业动态 更新时间:2024-10-09 19:14:46
本文介绍了是否可以使用逗号分隔变量和TSQL IN运算符过滤记录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可以使用带有TSQL IN运算符的逗号分隔变量过滤记录列表吗? 类似

Can a list of records be filtered using a comma delimited variable with the TSQL IN operator? like

declare @val nvarchar(max) set @val= 'E1+E2+E3' set @val=REPLACE(@val,'E','') set @val=REPLACE(@val,'+',',') select * from empmas where headcode in(@val)

推荐答案

是的它可以是例如 yes it can be for example select * from Employee where Salary in(5000,8000,15000,40000)

您可以使用广泛使用的fnsplitter函数拆分t sql ...你必须将这个功能添加到你的数据库... You can use the fnsplitter function which is used widely for splitting in t sql... you have to add this function to your database... USE [DatabaseName] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Function [dbo].[fnSplitter] (@IDs Varchar(4000) ) Returns @Tbl_IDs Table (ID nvarchar(100) COLLATE SQL_Latin1_General_CP1_CS_AS ) As Begin -- Append comma Set @IDs = @IDs + ',' -- Indexes to keep the position of searching Declare @Pos1 Int Declare @Pos2 Int -- Start from first character Set @Pos1=1 Set @Pos2=1 While @Pos1<len(@ids)> Begin Set @Pos1 = CharIndex(',',@IDs,@Pos1) Insert @Tbl_IDs Select Substring(@IDs,@Pos2,@Pos1-@Pos2) -- Go to next non comma character Set @Pos2=@Pos1+1 -- Search from the next charcater Set @Pos1 = @Pos1+1 End Return End

将您的查询更改为

after adding change your query to

Declare @val nvarchar(max) Set @val= 'E1+E2+E3' Set @val=REPLACE(@val,'E','') Set @val=REPLACE(@val,'+',',') Select * from empmas where headcode in(Select ID From fnSplitter(@Val))

更多推荐

是否可以使用逗号分隔变量和TSQL IN运算符过滤记录列表?

本文发布于:2023-11-09 03:59:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   可以使用   变量   运算符   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!