--创建数据
create table tab1
( id int , type varchar(50))
insert into tab1 
select 1 ,'1,111,2,3'
union select 2  ,'4,3'
union select 3 ,'5,2,1,11,111'create table tab2
(id int , type varchar(10))
insert into tab2
select 1, 'A'
union select 2 ,'B'
union select 3 ,'C'
union select 4 ,'C1'
union select 5 ,'E'
union select 11 ,'s'
union select 111 ,'t'--sql语句
update tab1 set type=','+type+','
declare @sql varchar(8000)
set @sql='type '
select @sql  ='replace('+@sql+','','+cast(id as varchar)+','','','+type+','')' from tab2
set @sql = 'update tab1 set type=' + @sql
select @sql
exec(@sql)
update tab1 set type = substring(type ,2,len(type)-2)--察看结果
select * from tab1
--删除测试数据
drop table tab1
drop table tab2tab11 1,111,2,3
2 4,3
3 5,2,1,11,111tab21 A
2 B
3 C
4 C1
5 E
11 s
111 t--结果
1 A,t,B,C
2 C1,C
3 E,B,A,s,t