我可以在 Dynamic SQL 中将多个字符串传递给一个参数吗?
Can I pass in several strings to one parameter in Dynamic SQL?
我正在尝试将多个字符串传递到一个名为“@Portfolio”的参数中,并使用 LIKE 子句。那可能吗?像这样。
Declare @AsOfDate varchar(10)
Declare @PID varchar(5)
Declare @Portfolio varchar(20)
Declare @sqlCommand varchar(max)
Set @AsOfDate = '04/30/2018'
Set @Portfolio = 'Treasury Investment,CASH,Derivatives,Wholesale Funding'
Set @PID = 'I.A.6'
...etc...
WHERE ((Tgt_DO.PID like ' + '''' + @PID + '''' + ' AND Tgt_DO.Portfolio like ' + '''' + @Portfolio + '''' + ' And Tgt_DO.AsOfDate = ' + '''' + @AsOfDate + '''' + '))'
我在 SQL Server 2008。
详细说明评论...由于您在某些情况下使用 LIKE
,因此您需要连接 %
where
Tgt_DO.PID like '%' + @PID + '%'
and Tgt_DO.Portfolio like '%' + @Portfolio + '%'
and Tgt_DO.AsOfDate = @AsOfDate
这 可能会 返回意想不到的结果,因为 @Portfolio
是逗号分隔的字符串。我会使用 Table-Valued Parameter or split them into a temp table or table variable using a splitter, like this answer
我正在尝试将多个字符串传递到一个名为“@Portfolio”的参数中,并使用 LIKE 子句。那可能吗?像这样。
Declare @AsOfDate varchar(10)
Declare @PID varchar(5)
Declare @Portfolio varchar(20)
Declare @sqlCommand varchar(max)
Set @AsOfDate = '04/30/2018'
Set @Portfolio = 'Treasury Investment,CASH,Derivatives,Wholesale Funding'
Set @PID = 'I.A.6'
...etc...
WHERE ((Tgt_DO.PID like ' + '''' + @PID + '''' + ' AND Tgt_DO.Portfolio like ' + '''' + @Portfolio + '''' + ' And Tgt_DO.AsOfDate = ' + '''' + @AsOfDate + '''' + '))'
我在 SQL Server 2008。
详细说明评论...由于您在某些情况下使用 LIKE
,因此您需要连接 %
where
Tgt_DO.PID like '%' + @PID + '%'
and Tgt_DO.Portfolio like '%' + @Portfolio + '%'
and Tgt_DO.AsOfDate = @AsOfDate
这 可能会 返回意想不到的结果,因为 @Portfolio
是逗号分隔的字符串。我会使用 Table-Valued Parameter or split them into a temp table or table variable using a splitter, like this answer