日期:2014-05-17 浏览次数:20439 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (aid int,bid int,cid int,PID int)
insert into [TB]
select 101,1001,2001,121 union all
select 101,1001,2002,121 union all
select 101,1001,2003,121 union all
select 101,1001,2001,122 union all
select 101,1001,2002,122 union all
select 101,1001,2003,122 union all
select 101,1002,2006,121 union all
select 101,1002,2007,121 union all
select 101,1002,2008,121 union all
select 102,1003,2009,123
select * from [TB]
DECLARE @a VARCHAR(100)
DECLARE @b VARCHAR(MAX)
DECLARE @c VARCHAR(MAX)
DECLARE @sql VARCHAR(MAX)
SET @a = '101'
SET @b = '1001,1002'
SET @c = '2001,2006,2008'
SET @sql = 'SELECT * FROM TB WHERE aid ='+ @a +'AND bid IN ('+@b+') AND cid IN ('+@c+')'
EXEC(@sql)
/*
aid bid cid PID
101 1001 2001 121
101 1001 2001 122
101 1002 2006 121
101 1002 2008 121*/
select * from [表名]
where (aid=101 and bid=1001 and cid in(2001,2002))
or(aid=101 and bid=1002 and cid in(2006,2007,2008))