emailVariable = john@example.com, sally@testing.com
SQL查詢:
select *
from [table_1]
where email in (?);
Parameter set to use emailVariable.
這不會回傳任何內容,兩封電子郵件都是有效的。
難道我做錯了什么?
我正在使用 OLE DB 源代碼編輯器。
uj5u.com熱心網友回復:
您還可以使用 string_split:
declare @stringToSplit varchar(255) = '[email protected], [email protected]'
select *
from [table_1]
where email in (
select ltrim(rtrim(value)) from string_split(?,',')
)
String_Split 將根據您的輸入字串和分隔符回傳一個值表。在您的 casse 中,由于額外的空間,您還需要 ltrim 和 rtrim。
uj5u.com熱心網友回復:
這是一個經典的錯誤。雖然以下有效:
where email in ('[email protected]','[email protected]')
您不能使用一個變數來放置多個值。逗號不是值字串的一部分,它被視為代碼。你可以做的是使用動態sql:
declare @emailVariable nvarchar(max)=N'''[email protected]'',''[email protected]''' -- notice the escaped quotes
declare @sql nvarchar(max)
set @sql=N'select * from [Table_1] where email in (' @emailVariable ')'
exec(@sql)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/353312.html
