变量:'103,101,107' 与 表中一个字段 值'101, 102, 103, 107' 进行比较 取字段包含所有变量的记录 sql 语句怎么写?
返回全部 包含的记录
------解决方案--------------------
- SQL code
create table tb(s varchar(20))insert into tb select '101,102,103,107'insert into tb select '102,103,107'insert into tb select '103,101,1077'declare @sql varchar(8000)set @sql='103,101,107'set @sql='select * into ## from (select '+replace(@sql,',',' as id union all select ')+')tp'exec(@sql)declare @where varchar(8000)select @where=isnull(@where+' and ','')+'charindex('','+ltrim(id)+','','',''+s+'','')>0'from ##exec('select * from tb where [email protected])