当前位置: 代码迷 >> Sql Server >> 请问:SQLSERVER中IN子查询能用变量么
  详细解决方案

请问:SQLSERVER中IN子查询能用变量么

热度:81   发布时间:2016-04-27 11:11:40.0
请教: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 ([email protected]_sz+')')
------解决方案--------------------
SQL code
--或者这样。declare @id_sz varchar(200) set @id_sz='70,73,76,05' select * from aTable where charindex(','+id_sz+',',',[email protected]_sz+',')>0
------解决方案--------------------
2楼也可,就是效率没有 1楼好.
  相关解决方案