以下是完整的SQL腳本,你的願望結果... 在這裏,我認爲
CREATE TABLE [dbo].[t1](
[id] [int] NULL,
[Result] [varchar](50) NULL
)
如表1 和
CREATE TABLE [dbo].[t2](
[id] [int] NULL,
[description] [nvarchar](50) NULL
)
如表2 和波紋管是完整的SQL服務器腳本。 ....
declare @Table table(Recid int identity, id int,Descp varchar(50))
create table #temp(id int,Descp varchar(50))
insert into @Table
select * from t1
declare @i int,@cnt int,@Str varchar(50),@Ids int,@StrQuery varchar(max)
select @cnt=count(*),@i=1 from @Table
while @i<[email protected]
begin
select @str='(' + Descp +')',@Ids=id from @Table where [email protected]
set @StrQuery=' insert into #temp select ' +cast(@Ids as varchar(10)) + ', description from t2 where cast(id as varchar(10)) in ' + @str
exec (@StrQuery)
set @[email protected]+1
end
declare @Table1 table(Recid int identity, id int,Descp varchar(50))
insert into @Table1(id)
select distinct id from #temp
declare @Id int
select @cnt=count(*),@i=1 from @Table1
while @i<[email protected]
begin
select @Id=id from @Table1 where RecID [email protected]
set @str =''
SELECT @str = COALESCE(@str + ',', '') + Descp
FROM #temp where [email protected]
update @Table1 set Descp [email protected] where [email protected]
set @[email protected]+1
end
select id,RIGHT(RTRIM(Descp), LEN(Descp) - 1) from @Table1
簡單。不要將任何內容存儲爲逗號分隔列表。這在各方面都很糟糕。 –
你真的把你的id存儲爲CSV,還是你的格式只能讓它看起來像這樣? –
[加入一臺基於逗號分隔值(http://stackoverflow.com/questions/26236436/joining-a-table-based-on-comma-separated-values) –