真不是一般奇葩的设计 create table A(ID int,客户名称 nvarchar(40),第四列 nvarchar(100),第五列 nvarchar(100)) insert into A select 140,'张三','1','|1||141|' union all select 141,'管理员','140','|1|140|' union all select 1,'李四','141','|1|140|141|'select distinct ID,客户名称 into temp from A
select ID,客户名称, 第四列=stuff((select '|'+ 客户名称 from temp where charindex('|' + rtrim(id)+'|','|' + A.第四列+'|')>0 for xml path('')),1,1,''), 第五列=stuff((select '|'+ 客户名称 from temp where charindex('|' + rtrim(id)+'|','|' + A.第五列+'|')>0 for xml path('')),1,1,'|') + '|' from A group by id,第四列,客户名称,第五列
如果用程序来写的话, “ 或者这样也可以,我在后台把数据查出来 用for循环挨个改成id对应的客户名称再拼装起来”,你拼装好了的, 然后重新写个数据集,不绑定以前的那个datatable,绑定你处理了的datatable。
create table A(ID int,客户名称 nvarchar(40),第四列 nvarchar(100),第五列 nvarchar(100))
insert into A
select 140,'张三','1','|1||141|' union all
select 141,'管理员','140','|1|140|' union all
select 1,'李四','141','|1|140|141|'select distinct ID,客户名称 into temp
from A
select ID,客户名称,
第四列=stuff((select '|'+ 客户名称 from temp
where charindex('|' + rtrim(id)+'|','|' + A.第四列+'|')>0 for xml path('')),1,1,''),
第五列=stuff((select '|'+ 客户名称 from temp
where charindex('|' + rtrim(id)+'|','|' + A.第五列+'|')>0 for xml path('')),1,1,'|') + '|'
from A group by id,第四列,客户名称,第五列
drop table A;
drop table temp;