【问题标题】:SSIS How to Use Parameters with IN ClauseSSIS 如何在 IN 子句中使用参数
【发布时间】:2021-12-21 00:53:01
【问题描述】:
emailVariable = john@example.com, sally@testing.com

SQL 查询:

select *
from [table_1]
where email in (?);

Parameter set to use emailVariable.

这不返回任何内容,两封电子邮件都有效。

我做错了吗?

我正在使用 OLE DB 源代码编辑器。

【问题讨论】:

标签: tsql variables ssis


【解决方案1】:

你也可以使用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 将根据您的输入字符串和分隔符返回一个值表。在您的情况下,由于额外的空格,您还需要 ltrim 和 rtrim。

【讨论】:

    【解决方案2】:

    这是一个典型的错误。虽然以下工作:

    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)
    

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 2018-06-08
      • 2013-02-07
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多