日期:2014-05-18 浏览次数:20478 次
create table demo(id int,[name] varchar(20),code varchar(20)) insert into demo select 3,'aa','1001' union all select 4,'bb','2001' union all select 5, 'cc','3001' union all select 6, '分类1','4001' union all select 7, '分类2','5001' select * from demo id name code ----------- -------------------- -------------------- 3 aa 1001 4 bb 2001 5 cc 3001 6 分类1 4001 7 分类2 5001
select * from demo where name in('1001','分类1','分类2') or code in('1001','分类1','分类2')
------解决方案--------------------
DECLARE @Str VARCHAR(1000) SET @Str = '1001,分类1,分类2' create table demo(id int,[name] varchar(20),code varchar(20)) insert into demo select 3,'aa','1001' union all select 4,'bb','2001' union all select 5, 'cc','3001' union all select 6, '分类1','4001' union all select 7, '分类2','5001' select * from demo WHERE CHARINDEX(',' + name + ',',',' + @Str + ',') > 0 OR CHARINDEX(',' + code + ',',',' + @Str + ',') > 0 /* id name code 3 aa 1001 6 分类1 4001 7 分类2 5001 */
------解决方案--------------------