34,594
社区成员
发帖
与我相关
我的任务
分享
SELECT * FROM tablename WHERE 12 IN('12','13')
--exec示例
if object_id('[tablename]') is not null drop table [tablename]
create table [tablename] (id int,name varchar(1))
insert into [tablename]
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select 6,'f'
DECLARE @i VARCHAR(30) SET @i='1,4'
EXEC('select * from [tablename] WHERE id IN ('+@i+')')
/*
id name
----------- ----
1 a
4 d
*/
--charindex示例
if object_id('[tablename]') is not null drop table [tablename]
create table [tablename] (id int,name varchar(1))
insert into [tablename]
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select 6,'f'
DECLARE @i VARCHAR(30) SET @i='1,4'
SELECT * FROM [tablename]
WHERE CHARINDEX(','+LTRIM(id)+',',','+@i+',')>0
/*
id name
----------- ----
1 a
4 d
*/