日期:2014-05-19 浏览次数:20851 次
insert into b(id) select uaserid from A union all select uaserid1 from A union all select uaserid2 from A union all select uaserid3 from A
------解决方案--------------------
if object_id('A') is not null drop table A go
select 1 as uaserid,3 as uaserid1,4 as uaserid2,5 as uaserid3 into A
if object_id('B') is not null drop table B go
select a.uaserid1 as ID
into B
from (select uaserid1 from A union all
select uaserid2 from A union all
select uaserid3 from A) a
select * from B
------解决方案--------------------
insert into b(id) select uaserid1 from A union all select uaserid2 from A union all select uaserid3 from A
------解决方案--------------------
create function [dbo].[m_split](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(200))
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
--定义@表A,@表B
declare @表A table (uaserid int,uaserid1 nvarchar(20),uaserid2 nvarchar(20),uaserid3 nvarchar(20))
insert into @表A
select 1,3,4,5
declare @表B table (id int)
declare @t varchar(20)
select @t= uaserid1+','+uaserid2+','+uaserid3 from @表A
--插入数据
insert into @表B
select * from dbo.m_split(@t,',')
--查询结果
select * from @表B
/*
id
-----------
3
4
5
*/