SSIS 如何在 IN 子句中使用参数
SSIS How to Use Parameters with IN Clause
emailVariable = john@example.com, sally@testing.com
SQL 查询:
select *
from [table_1]
where email in (?);
Parameter set to use emailVariable.
这个returns没什么,两封邮件都有效。
我是不是做错了什么?
我正在使用 OLE DB 源代码编辑器。
这是一个典型的错误。虽然以下工作:
where email in ('john@example.com','sally@testing.com')
您不能使用一个变量来放置多个值。逗号不是值字符串的一部分,它被视为代码。你可以做的是使用动态 sql:
declare @emailVariable nvarchar(max)=N'''john@example.com'',''sally@testing.com''' -- notice the escaped quotes
declare @sql nvarchar(max)
set @sql=N'select * from [Table_1] where email in (' + @emailVariable + ')'
exec(@sql)
你也可以使用string_split:
declare @stringToSplit varchar(255) = 'john@example.com, sally@testing.com'
select *
from [table_1]
where email in (
select ltrim(rtrim(value)) from string_split(?,',')
)
String_Split 将根据您输入的字符串和定界符 return table 个值。在你的情况下,你还需要 ltrim 和 rtrim 因为额外的空间。
emailVariable = john@example.com, sally@testing.com
SQL 查询:
select *
from [table_1]
where email in (?);
Parameter set to use emailVariable.
这个returns没什么,两封邮件都有效。
我是不是做错了什么?
我正在使用 OLE DB 源代码编辑器。
这是一个典型的错误。虽然以下工作:
where email in ('john@example.com','sally@testing.com')
您不能使用一个变量来放置多个值。逗号不是值字符串的一部分,它被视为代码。你可以做的是使用动态 sql:
declare @emailVariable nvarchar(max)=N'''john@example.com'',''sally@testing.com''' -- notice the escaped quotes
declare @sql nvarchar(max)
set @sql=N'select * from [Table_1] where email in (' + @emailVariable + ')'
exec(@sql)
你也可以使用string_split:
declare @stringToSplit varchar(255) = 'john@example.com, sally@testing.com'
select *
from [table_1]
where email in (
select ltrim(rtrim(value)) from string_split(?,',')
)
String_Split 将根据您输入的字符串和定界符 return table 个值。在你的情况下,你还需要 ltrim 和 rtrim 因为额外的空间。