これを行うには、分割によってUser Define Function
を検出Country
し、2 番目のテーブルに挿入する を作成します。
City
Address
分割機能 :
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (id int , items varchar(8000))
as
begin
declare @idx int
declare @id int
declare @slice varchar(8000)
set @id = 1
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
begin
insert into @temptable(id, Items) values( @id , @slice)
set @id = @id + 1
end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
次に、次のように挿入クエリで使用します。
INSERT INTO NewTbl
SELECT .... ,
(SELECT ITEMS FROM dbo.Split(Address) where id = 1) as Country,
(SELECT ITEMS FROM dbo.Split(Address) where id = 2) as City,
(SELECT ITEMS FROM dbo.Split(Address) where id = 3) as Address,
...