日期:2014-05-17  浏览次数:20427 次

请教:SQLSERVER中IN子查询能用变量么?
declare @id_sz varchar(200)
set @id_sz='70,73,76,05'
select * from aTable where id_sz in (@id_sz) //这样查询不到信息

//如果用拼SQL语句的方式则或以,但我不能用那种方式

------解决方案--------------------
--这样,用动态SQL.
SQL code
declare @id_sz varchar(200) 
set @id_sz='70,73,76,05' 
exec('select * from aTable where id_sz in ('+@id_sz+')')

------解决方案--------------------
SQL code
--或者这样。
declare @id_sz varchar(200) 
set @id_sz='70,73,76,05' 
select * from aTable where charindex(','+id_sz+',',','+@id_sz+',')>0

------解决方案--------------------
2楼也可,就是效率没有 1楼好.