问一个sql 字符串 处理的问题
- SQL code
/* 这是一组 测试数据Id 角色1 业务员2 业务员3 业务员4 业务员5 业务员,访客6 业务员7 业务员8 业务员*/现在有一个 字符串: '业务经理,访客'我的问题就是 让 字符串 和 角色列里的 每个数据逐一和这个字符串 比对, 比如: 业务经理 在不在 角色列里面,然后 访客在不在 角色列里面,然后 找到了 id=5 的 数据 里面有一个 访客数据, 排除这个数据 得出的结果:Id 角色1 业务员2 业务员3 业务员4 业务员6 业务员7 业务员8 业务员这 就是 我想要的 ,给点 思路就好,
[解决办法]
- SQL code
select * from t1 a where not exists( select 1 from t1 where CHARINDEX('访客',角色)>0 and CHARINDEX('业务员',角色)>0 and a.角色=角色)
[解决办法]
- SQL code
declare @s varchar(100)set @s = '业务经理,访客'set @s = replace(@s,',',''' as t union all select''')select 'select '''+@s+''''exec('SELECT a.* FROM TA aleft join ('+'select '''+@s+''''+') bon patindex(''%''+b.t+''%'',[角色])>0where t is null')
[解决办法]
- SQL code
create table #t( Id int,[角色] nvarchar(100))insert into #tselect 1,'业务员' union allselect 2,'业务员' union allselect 3,'业务员' union allselect 4,'业务员' union allselect 5,'业务员,访客' union allselect 6,'业务员' union allselect 7,'业务员' union allselect 8,'业务员'select * from #t where CHARINDEX (',业务经理,',',' +[角色] + ',' )=0 and CHARINDEX (',访客,',',' +[角色] + ',' )=0Id 角色----------- ----------------------------------------------------------------1 业务员2 业务员3 业务员4 业务员6 业务员7 业务员8 业务员