declare @tb1 table(id varchar(10),f1 varchar(20),f2 varchar(20),f3 varchar(20))
declare @tb2 table(id varchar(10),fd1 varchar(20),fd2 varchar(20),fd3 varchar(20))
declare @tb3 table(id varchar(10),fed1 varchar(20),fed2 varchar(20),fed3 varchar(20))
insert @tb1(id,f1,f2,f3)
select '01','hh','gg','jj'
union
select '02','kk','gt','ft'
insert into @tb2(id,fd1,fd2,fd3)
select '01', 'jjh', 'kkk', 'hhh'
union
select '01', 'juh', 'hhh', 'ghj'
union
select '02', 'kkh', 'bbn', 'hjb'
union
select '02', 'hhg', 'tty', 'ghb'
insert into @tb3(id,fed1,fed2,fed3)
select '01', 'jkk', 'mjkj', 'kkkk'
union
select '01', 'kkk', 'llll', 'lllk'
union
select '02', 'jjm', 'jkjjk', 'kjkj'
union
select '02', 'jjj', 'jkjkk', 'kjk'
union
select '02', 'nbjh', 'jk1', '89j' select top 0 a.id,a.f1,a.f2,a.f3,b.fd1,b.fd2,b.fd3,c.fed1,c.fed2,c.fed3
into #temp
from @tb1 a left join @tb2 b on a.id = b.id
left join @tb3 c on a.id = c.iddeclare @id varchar(10),@f1 varchar(20),@f2 varchar(20),@f3 varchar(30)
declare c cursor for
select id,f1,f2,f3 from @tb1
open c
while(0=0)
begin
fetch next from c into @id,@f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(id,f1,f2,f3) values(@id,@f1,@f2,@f3)
declare c1 cursor for
select fd1,fd2,fd3 from @tb2 where id = @id
open c1
while(0=0)
begin
fetch next from c1 into @f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(fd1,fd2,fd3) values(@f1,@f2,@f3)
end
close c1
deallocate c1
declare c2 cursor for
select fed1,fed2,fed3 from @tb3 where id = @id
open c2
while(0=0)
begin
fetch next from c2 into @f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(fed1,fed2,fed3) values(@f1,@f2,@f3)
end
close c2
deallocate c2
end
close c
deallocate cselect
isnull(id,'') id,
isnull(f1,'') f1,
isnull(f2,'') f2,
isnull(f3,'') f3,
isnull(fd1,'') fd1,
isnull(fd2,'') fd2,
isnull(fd3,'') fd3,
isnull(fed1,'') fed1,
isnull(fed2,'') fed2,
isnull(fed3,'') fed3
from #tempdrop table #temp
declare @tb2 table(id varchar(10),fd1 varchar(20),fd2 varchar(20),fd3 varchar(20))
declare @tb3 table(id varchar(10),fed1 varchar(20),fed2 varchar(20),fed3 varchar(20))
insert @tb1(id,f1,f2,f3)
select '01','hh','gg','jj'
union
select '02','kk','gt','ft'
insert into @tb2(id,fd1,fd2,fd3)
select '01', 'jjh', 'kkk', 'hhh'
union
select '01', 'juh', 'hhh', 'ghj'
union
select '02', 'kkh', 'bbn', 'hjb'
union
select '02', 'hhg', 'tty', 'ghb'
insert into @tb3(id,fed1,fed2,fed3)
select '01', 'jkk', 'mjkj', 'kkkk'
union
select '01', 'kkk', 'llll', 'lllk'
union
select '02', 'jjm', 'jkjjk', 'kjkj'
union
select '02', 'jjj', 'jkjkk', 'kjk'
union
select '02', 'nbjh', 'jk1', '89j' select top 0 a.id,a.f1,a.f2,a.f3,b.fd1,b.fd2,b.fd3,c.fed1,c.fed2,c.fed3
into #temp
from @tb1 a left join @tb2 b on a.id = b.id
left join @tb3 c on a.id = c.iddeclare @id varchar(10),@f1 varchar(20),@f2 varchar(20),@f3 varchar(30)
declare c cursor for
select id,f1,f2,f3 from @tb1
open c
while(0=0)
begin
fetch next from c into @id,@f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(id,f1,f2,f3) values(@id,@f1,@f2,@f3)
declare c1 cursor for
select fd1,fd2,fd3 from @tb2 where id = @id
open c1
while(0=0)
begin
fetch next from c1 into @f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(fd1,fd2,fd3) values(@f1,@f2,@f3)
end
close c1
deallocate c1
declare c2 cursor for
select fed1,fed2,fed3 from @tb3 where id = @id
open c2
while(0=0)
begin
fetch next from c2 into @f1,@f2,@f3
if(@@fetch_status<>0) break
insert into #temp(fed1,fed2,fed3) values(@f1,@f2,@f3)
end
close c2
deallocate c2
end
close c
deallocate cselect
isnull(id,'') id,
isnull(f1,'') f1,
isnull(f2,'') f2,
isnull(f3,'') f3,
isnull(fd1,'') fd1,
isnull(fd2,'') fd2,
isnull(fd3,'') fd3,
isnull(fed1,'') fed1,
isnull(fed2,'') fed2,
isnull(fed3,'') fed3
from #tempdrop table #temp
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货