在 SQL Server 2008 上,我有一个变量 @storename,它是一个 varchar(20) 和这个语句:
SET @query = 'select '+@result1+' '+@colsNull+' into ##tempz
FROM
(SELECT '+@result2+'
FROM rpPay p LEFT JOIN RPTrs r ON p.ReceiptNo = r.receiptno LEFT JOIN Currencies c ON LEFT(p.paytype,1) = c.POSCurrency
WHERE r.trsdate >= '''+ convert(varchar(10), @startDate, 101) +'''
AND r.trsdate <= '''+ convert(varchar(10), @endDate, 101) +'''
) p
pivot
(sum(amount) FOR CurrencyDesc in('+@cols+')) piv'
execute(@query)
我正在尝试将 where 子句设置为所有 StoreID 或 @storename 的内容。我可以在那里有一个 IF 或 case 语句或类似的东西
and r.StoreID = * or r.StoreID = @storename
有没有在 where 子句中使用通配符的事情?
就目前而言,我已经尝试过:
declare @storename varchar(20)
set @storename = '01'
and ('+@storename+' is null or r.storeId = '+@storename+') -- Error : Conversion failed when converting the varchar value 'BK' to data type int.
and r.StoreID like '+@storename+' -- Returns no information, just blank table
也刚试过
declare @storename varchar(20)
set @storename = '01'
and r.storeid = '''+@storename+'''
哪个确实有效,并且只从商店 01 退回所有东西,但是如果我放
set @storename = null -- Gives me invalid Object name for the temp table I'm using
set @storename = '' -- give back no results again, just empty table