加载数据
CREATE TABLE #tb
(id VARCHAR(10))
INSERT INTO #tb
SELECT '1'
UNION ALL
SELECT '2'
UNION all
SELECT '3'
UNION ALL
SELECT '4'
UNION
SELECT '5'
declare @code varchar(5000)
set @code=',1,2,3,4'
SELECT REPLACE(''''+@code+'''',',',''',''')
SELECT * FROM #TB WHERE id IN (REPLACE(''''+@code+'''',',',''',''') )
----得不出数据
SELECT * FROM #TB WHERE id IN('','1','2','3','4')
---只有这样才可以得到数据
各位大侠,这是什么原因呀。 数据
[解决办法]
declare @code varchar(5000)
set @code=',1,2,3,4'
SELECT REPLACE(''''+@code+'''',',',''',''')--不能用这种写法,
SELECT * FROM #TB WHERE id IN (REPLACE(''''+@code+'''',',',''',''') )
--这行语句相当于:
SELECT * FROM #TB WHERE id IN (''''',''1'',''2'',''3'',''4''')
SELECT * FROM #TB WHERE id IN (@某一变量)--它会把你的整个字符串看成一个值,而不是"空,1,2,3,4"多个值