Re: [SQL ] 將字串串起來的SQL語法?

看板Database作者 (bero)時間13年前 (2012/08/14 12:07), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
DECLARE @group TABLE ( [values] varchar(8000), [group] int ) if exists (select * from tempdb.sys.tables t where t.name like '#groups%') drop table dbo.#groups ;with a(value, [group]) as ( select * from (values ('a', 1), ('b', 1), ('c', 1), ('d', '2'), ('e', 2)) a(value, [group]) ) select * into #groups from a --select * from #groups insert into @group select distinct null, [group] from #groups declare @g int declare cur cursor for select [group] from @group open cur fetch next from cur into @g WHILE(@@FETCH_STATUS=0) BEGIN declare @str varchar(8000) = '' select @str += ' ' + value from #groups where [group] = @g --select @str a update @group set [values] = @str where [group] = @g fetch next from cur into @g END CLOSE CUR DEALLOCATE CUR select * from @group -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.63.100.2

08/14 14:54, , 1F
謝謝
08/14 14:54, 1F
文章代碼(AID): #1GASyCIm (Database)
文章代碼(AID): #1GASyCIm (Database)