这是我们正在进一步处理的样本数据。
create table #tmp (id int identity(1,1), na varchar(10),me varchar(10))
insert into #tmp (na,me)
values
('a','t'),
('a','u'),
('a','v'),
('a','w'),
('b','x'),
('b','y'),
('b','z')
select * from #tmp
我的问题是,STRING_AGG
SQL Server 的功能有什么完全相反的吗?
就像我正在使用STRING_AGG
以下代码合并
select na, STRING_AGG(me,',') as me into #tmp1 from #tmp group by na
select * from #tmp1
我需要扭转这个过程,但我必须使用 CURSOR,所以我正在寻找替代解决方案。
下面是光标代码,以便更好地理解目的。
declare @na varchar(10)
declare @me varchar(max)
create table #tmp3 (na varchar(10),me varchar(10))
declare dbc cursor for select na, me from #tmp1
open dbc
while 1=1
begin
fetch next from dbc into @na, @me
if @@FETCH_STATUS <> 0
break;
insert into #tmp3 (na,me)
select @na, value
from string_split(@me,',')
end
close dbc
deallocate dbc
select * from #tmp3
--Delete temp table
drop table #tmp
drop table #tmp1
drop table #tmp3