如何合并表
表test中有两个字段A,B
A B
ABX,CDN,FFF GGG,TTT,HHH
要求合并A和B2字段
A B
ABX GGG
ABX TTT
ABX HHH
CDN GGG
CDN TTT
CDN HHH
FFF GGG
FFF TTT
FFF HHH
谢谢!
------解决方案--------------------if exists (select * from dbo.sysobjects where id = object_id(N '[dbo].[fn_split] ') and xtype in (N 'FN ', N 'IF ', N 'TF '))
drop function [dbo].[fn_split]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
/*
功能:实现split功能的函数
*/
create function fn_split
(
@inputstr varchar(8000),
@seprator varchar(10)
)
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator, @inputstr)
while @i > = 1
begin
insert @temp values(left(@inputstr, @i - 1))
set @inputstr = substring(@inputstr, @i + 1, len(@inputstr) - @i)
set @i = charindex(@seprator, @inputstr)
end
if @inputstr <> '\ '
insert @temp values(@inputstr)
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
declare @s1 varchar(1000),@s2 varchar(1000)
set @s1= 'ABX,CDN,FFF '
set @s2= 'GGG,TTT,HHH '
select t1.a as A,t2.a as B
from
(select * from dbo.fn_split(@s1, ', ') ) as t1
cross join
(select * from dbo.fn_split(@s2, ', ') ) as t2
order by a,b