日期:2014-05-18 浏览次数:20732 次
declare @A表 table (c1 varchar(1),c2 varchar(8)) insert into @A表 select 'a',null union all select 'b',null union all select 'c',null union all select 'd',null declare @B表 table (id int,c1 varchar(10)) insert into @B表 select 1,'a' union all select 2,'a,b,c' union all select 3,'a,c' update @A表 set c2=case when b.id is null then 'No' else 'Yes' end from @A表 a left join @B表 b on charindex(','+a.c1+',',','+b.c1+',')>0 select * from @A表 /* c1 c2 ---- -------- a Yes b Yes c Yes d No */
------解决方案--------------------
USE CSDN GO declare @A表 table (c1 varchar(1),c2 varchar(8)) insert into @A表 select 'a',null union all select 'b',null union all select 'c',null union all select 'd',null declare @B表 table (id int,c1 varchar(10)) insert into @B表 select 1,'a' union all select 2,'a,b,c' union all select 3,'a,c' UPDATE A SET c2 = (CASE WHEN EXISTS(SELECT 1 FROM @B表 B WHERE CHARINDEX(','+A.c1+',', ','+B.c1+',') > 0) THEN 'YES' ELSE 'NO' END) FROM @A表 A WHERE c2 IS NULL /* c1 c2 a YES b YES c YES d NO */