使用動態IN子句爲什麼下面的SQL不取我什麼在MSSQL
DECLARE @Status AS VARCHAR(400)
SET @status = '''Closed'',''OPEN'''
select * from MYTABLE where status in(@status)
雖然作爲 select * from MYTABLE where status in('Closed','Open')
取我行
使用動態IN子句爲什麼下面的SQL不取我什麼在MSSQL
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,但我認爲它不是真正的競爭力..
DECLARE @Status nVARCHAR(400),
@SQL nvarchar(500)
SET @status = '''Closed'''+','+'''OPEN'''
set @SQL = '
select * from [MYTABLE] where status in('[email protected] +')'
exec sp_executesql @SQL
GO
你的第一個問題,檢查值'Closed','OPEN'
存在於數據庫中。值不會擴大。
如果您使用SQL Server 2008或更高版本,則可以使用Table Valued Parameters來實現相同的目的。
感謝您的解釋,永遠不知道。幫助我理解問題 – rsapru 2011-03-04 10:59:14
我做了檢查,但他們均與LINQ。對不起,如果我錯過了任何,請給我指向類似的帖子 – rsapru 2011-03-04 10:41:21
檢查此鏈接可能會有所幫助。嗯你是對的。看起來很難找到一個,但我相信有很多。無論如何有一個鏈接艱難 - http://stackoverflow.com/questions/2194120/formatting-a-string-for-a-sql-in-clause – 2011-03-04 10:54:45