![](/img/trans.png)
[英]How do I update a column of a table which picks values from a default null valued column of another table?
[英]How to fetch values from CSV valued column
表格1
Column A Column B Column C Column D =========================================================== 1 2 3 a,b,c
形成上表,我需要输出如下所示:
Result from query ----------------- Column A Column B Column C Column D =========================================================== 1 2 3 a 1 2 3 b 1 2 3 c
这是您的问题的通用解决方案,希望它会有所帮助...
declare @t table
(
ID int identity(1,1),
columnA int,
columnB int,
columnC int,
columnD varchar(20)
)
insert into @t select 1,2,3,'a,b,c' union all select 4,5,6,'a,b,d' union all select 7,8,9,'a'
--select * from @t
declare @tt table
(
ID int identity(1,1),
columnA int,
columnB int,
columnC int,
columnD varchar(20)
)
declare @count int
set @count = (select count(*) from @t)
declare @i int = 1
declare @str varchar(20)
while (@i <= @count)
BEGIN
set @str = (select columnD from @t where ID = @i)
while charindex(',' , @str) <> 0
begin
Insert @tt (columnA, columnB, columnC, columnD) select columnA, columnB, columnC, left(@str, charindex(',' , @str) - 1) from @t where ID = @i
SET @str = stuff(@str, 1, charindex(',' , @str), '')
end
Insert @tt (columnA, columnB, columnC, columnD) select columnA, columnB, columnC, @str from @t where ID = @i
SET @i = @i + 1
END
select * from @tt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.