您可以通过创建一个User Define Function
检测Country
, 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,
...