过滤“IN”中的字段使用变量的子句

发布于 2024-12-21 11:58:31 字数 320 浏览 0 评论 0原文

我想做这样的事情:

DECLARE @list nvarhcar(200) SET @list = 'VALUE1,VALUE2,VALUE3'

Select * from foo where field in (@list)

直到今天我使用 sp_executeSQL 过程、我自己的数据库函数 csv2table 和 subselect 解决了问题,但我认为这不是很干净。

有什么办法可以不使用动态sql来解决吗?有直接的方法吗?

我正在使用 Microsoft Sql Server 2005。

谢谢!

I would like to do something like this :

DECLARE @list nvarhcar(200) SET @list = 'VALUE1,VALUE2,VALUE3'

Select * from foo where field in (@list)

Until today I solved using sp_executeSQL procedure, my own database function csv2table and subselect, but I think that is not very clean.

Is there any way to solve without use dynamic sql ? Is there a direct method?

I'm using Microsoft Sql Server 2005.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷月断魂刀 2024-12-28 11:58:31

请您尝试如下:谢谢

DECLARE @list nvarchar(200) 
SET @list = 'VALUE1,VALUE2,VALUE3'

SELECT * FROM foo WHERE CHARINDEX(',' + field + ',', ',' + @list + ',') > 0 

Would you please try as below: thanks

DECLARE @list nvarchar(200) 
SET @list = 'VALUE1,VALUE2,VALUE3'

SELECT * FROM foo WHERE CHARINDEX(',' + field + ',', ',' + @list + ',') > 0 
青芜 2024-12-28 11:58:31

将 A 部分创建为 UDF

/* Part A */
DECLARE @list Varchar(max) = 'VALUE1,VALUE2,VALUE3'
DECLARE @tmpTbl_Values Table  (ID varchar(50)) 

Set @list =  @list + ',' 
-- 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(@list)
Begin
Set @Pos1 = CharIndex(',',@list,@Pos1)
Insert @tmpTbl_Values Select  Cast(Substring(@list,@Pos2,@Pos1-@Pos2) As varchar(50))
-- Go to next non comma character
Set @Pos2=@Pos1+1
-- Search from the next charcater
Set @Pos1 = @Pos1+1
End 


/* Part B */
SELECT      * 
FROM        foo A
INNER JOIN  @tmpTbl_Values B
ON          AB.ID = B.ID

Create Part A as an UDF

/* Part A */
DECLARE @list Varchar(max) = 'VALUE1,VALUE2,VALUE3'
DECLARE @tmpTbl_Values Table  (ID varchar(50)) 

Set @list =  @list + ',' 
-- 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(@list)
Begin
Set @Pos1 = CharIndex(',',@list,@Pos1)
Insert @tmpTbl_Values Select  Cast(Substring(@list,@Pos2,@Pos1-@Pos2) As varchar(50))
-- Go to next non comma character
Set @Pos2=@Pos1+1
-- Search from the next charcater
Set @Pos1 = @Pos1+1
End 


/* Part B */
SELECT      * 
FROM        foo A
INNER JOIN  @tmpTbl_Values B
ON          AB.ID = B.ID
在巴黎塔顶看东京樱花 2024-12-28 11:58:31

一种替代方案:

Select * from foo where '%,' + field + ',%' like ',' + (@list) + ','

这比现有方法甚至“更脏”。

理想情况下,我建议将查询字符串更改为:

Select * from foo where field in ()

然后读取 @list 中的值列表,为每个值插入 ? (用逗号分隔)并将每个值绑定到该参数标记。但是,我不知道如何单独用动态 SQL 来实现这一点。

One alternative:

Select * from foo where '%,' + field + ',%' like ',' + (@list) + ','

This is even "dirtier" than the existing approach.

Ideally, I would suggest changing the query string to:

Select * from foo where field in ()

then reading through the list of values in @list, inserting a ? for each value (separated by commas) and binding each value to that parameter marker. However, I don't know how to achieve this in dynamic SQL alone.

终难愈 2024-12-28 11:58:31

如果字段的数据类型是 nchar 或 varchar,则 IN() 运算符将查找类似的值,

IN('VALUE1','VALUE2','VALUE3', etc..)            

因此您可以根据该值设置 @list

If the datatype for the field is nchar or varchar, the IN() operator will look for value like

IN('VALUE1','VALUE2','VALUE3', etc..)            

So you set your @list based on that

请止步禁区 2024-12-28 11:58:31

一种可能的方法是执行以下操作:

SELECT * FROM foo WHERE CHARINDEX(',' + field + ',', ',' + @list + ',') > 0 

假设字段中的数据中没有逗号

A possible approach is to do the following:

SELECT * FROM foo WHERE CHARINDEX(',' + field + ',', ',' + @list + ',') > 0 

This is assuming that the data in the field has no comma in it

格子衫的從容 2024-12-28 11:58:31

如果你处理字符串字段,你可以使用这样的东西..

DECLARE @csv varchar(50) = 'item1,item2, item3, item4 ,item5'
SELECT * FROM foo WHERE PATINDEX('%' + Field + '%', @csv) > 0

If you deal with a string field, you could use something like this..

DECLARE @csv varchar(50) = 'item1,item2, item3, item4 ,item5'
SELECT * FROM foo WHERE PATINDEX('%' + Field + '%', @csv) > 0
哥,最终变帅啦 2024-12-28 11:58:31

在 SQL 2005 中,您可以使用自定义 UDF 将分隔列表解析为表 - 请参阅这篇文章 http://www.sommarskog.se/arrays-in-sql-2005.html

In SQL 2005 you can use a custom UDF to parse a delimited list into a table - please see this article http://www.sommarskog.se/arrays-in-sql-2005.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文