|
发表于 2020-1-29 23:00:01
|
显示全部楼层
create table T(Menu_Name varchar(3), Action_Name varchar(4) )
insert T select 'A01', '新增'
union all select 'A01', '修改'
union all select 'A01', '刪除'
union all select 'A01', '審核'
union all select 'B01', '新增'
union all select 'B01', '修改'
union all select 'C01', '刪除'
union all select 'C01', '審核'
alter table t add 显示列 varchar(20)--新增显示列
go
declare @ta table(id int identity(1,1),Menu_Name varchar(3),Action_Name varchar(4))
insert @ta
select Menu_Name,Action_Name from T
while exists(select 1 from @ta)
begin
update t
set 显示列=isnull(显示列+',','')+ta.Action_Name
from @ta ta,t
where t.Menu_Name=ta.Menu_Name and
not exists(select 1 from @ta where Menu_Name=ta.Menu_Name and id<ta.id)
delete ta
from @ta ta
where not exists(select 1 from @ta where Menu_Name=ta.Menu_Name and id<ta.id)
end
go
select distinct Menu_Name,显示列 from t--查询
go
alter table t drop column 显示列--删除
Menu_Name 显示列
--------- --------------------
A01 新增,修改,刪除,審核
B01 新增,修改
C01 刪除,審核
(所影响的行数为 3 行)
|
|