小弟现在有一个这样的需求,下列id都是自增长的,存在A表,数据如下
A表
ID name_list
1 昌平路1号/昌平路11号/昌平路111号
2 西康路520号
3 陕西北路1弄/陕西北路111弄现在要将A表中的数据插入到B表,结果应该为B表
ID name
1 昌平路1号
2 昌平路11号
3 昌平路111号
4 西康路520号
5 陕西北路1弄
6 陕西北路111弄在A表中的name_list字段内容会以‘/’形式分割开
哪位大侠高手教教我sql语句怎么写啊,跪求~~~~~~~~
A表
ID name_list
1 昌平路1号/昌平路11号/昌平路111号
2 西康路520号
3 陕西北路1弄/陕西北路111弄现在要将A表中的数据插入到B表,结果应该为B表
ID name
1 昌平路1号
2 昌平路11号
3 昌平路111号
4 西康路520号
5 陕西北路1弄
6 陕西北路111弄在A表中的name_list字段内容会以‘/’形式分割开
哪位大侠高手教教我sql语句怎么写啊,跪求~~~~~~~~
if Object_id('f_split') is not null
begin
drop function f_split
end
gocreate function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col)values (@c)
return
end
go print 'ok'/*生成临时表*/
create table #temp1
(
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](500) null
)
goinsert into #temp1 ([name]) values ('昌平路1号/昌平路11号/昌平路111号')
insert into #temp1 ([name]) values ('西康路520号')
insert into #temp1 ([name]) values ('陕西北路1弄/陕西北路111弄')/*执行*/
declare @Sql nvarchar(max)set @sql = 'insert into tableb ([name]) select distinct col from (select null as col '
select @sql = @sql + ' union all select * from dbo.f_split(''' + [name] + ''',''/'')'
from (select [name] from #temp1) b
set @sql = @sql + ')c where c.col is not null'
print @Sql
print len(@Sql)exec(@sql)
godrop table #temp1
go