在 MSSQL 中使用动态 IN 子句 [英] using dynamic IN clause in MSSQL
本文介绍了在 MSSQL 中使用动态 IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么下面的 SQL 没有给我任何东西
Why the following SQL does not fetch me anything
DECLARE @Status AS VARCHAR(400)
SET @status = '''Closed'',''OPEN'''
select * from MYTABLE where status in(@status)
虽然select * from MYTABLE where status in('Closed','Open')
获取我的行
推荐答案
如果你想做一些动态 SQL,你可以,但我认为它没有真正的竞争力..
You can if you want do some dynamic SQL but I think it is not really competitive..
DECLARE @Status nVARCHAR(400),
@SQL nvarchar(500)
SET @status = '''Closed'''+','+'''OPEN'''
set @SQL = '
select * from [MYTABLE] where status in('+@status +')'
exec sp_executesql @SQL
GO
这篇关于在 MSSQL 中使用动态 IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文