现在要把表B里的CompanyName Title的内容放进表A里去,条件是UID相等
而且,表B里UID有重复的情况
create table tableA
(
[UID] int identity(1,1) primary key,
CompanyName varchar(50),
Title varchar(50)
)
create table tableB
(
CompanyName varchar(50),
Title varchar(50),
UID int
)
insert into tableA(CompanyName,Title)
(
select 'aa','aa' union
select 'bb','bb' union
select 'cc','cc' union
select 'dd','dd' union
select 'ee','ee'
)
insert into tableB(CompanyName,Title,[UID])
(
select 'hh','hh',1 union
select 'ii','ii',1 union
select 'jj','jj',3 union
select 'kk','kk',4 union
select 'll','ll',5
)
select * from tableA
go
select * from tableB
go
declare @CompanyName varchar(50)
declare cur cursor for
select B.CompanyName from tableA A,tableB B where A.UID=B.UID and B.UID in(select [UID] from tableA)
open cur
Fetch next From Cur into @CompanyName
while @@fetch_status=0
Begin
declare @Title varchar(50)
select @Title=B.Title from tableA A,tableB B where A.UID=B.UID and B.CompanyName=@CompanyName and B.UID in(select [UID] from tableA)
insert into tableA(CompanyName,Title)
values(@CompanyName,@Title)
Fetch Next From Cur Into @CompanyName
end
close cur
Deallocate Cur
go