CREATE PROCEDURE p1 AS
declare c1 cursor local for
select AA from 表A where BB=你要查的值
declare @AA varchar(25)
declare @i int
Open c1
Fetch next from c1 into @AA
while @@Fetch_Status=0
begin
select @i=count(*)from 表B where AA=@AA
if@i=0
begin
insert into 表B select * from 表A where AA=@AA
end
else
begin
update 表B set BB=t.BB,CC=t.CC,DD=t.DD from 表A t inner join 表B u on t.AA=u.AA
end
Fetch next from c1 into @AA
end
Close c1
Deallocate c1
GO
CREATE PROCEDURE p1 AS
declare c1 cursor local for
select AA from 表A where BB=你要查的值
declare @AA varchar(25)
declare @i int
Open c1
Fetch next from c1 into @AA
while @@Fetch_Status=0
begin
select @i=count(*)from 表B where AA=@AA
if@i=0
begin
insert into 表B select * from 表A where AA=@AA
end
else
begin
update 表B set BB=t.BB,CC=t.CC,DD=t.DD from 表A t inner join 表B u on t.AA=u.AA
end
end
Close c1
Deallocate c1
GO
create procedure sp_test(@BB int)
as
begin
if exists(select 1 from A,B where A.AA=B.AA and A.BB=@BB)
update B
set
BB=A.BB,CC=A.CC,DD=A.DD
from
A,B
where
A.AA=B.AA and A.BB=@BB
else
insert into B select * from A where BB=@BB
end
go
修改:
if not exists(select * from B,A where B.AA=A.AA and A.bb=@bb)
insert into bb select * from a where a.bb=@bb
else
update B set bb= A.bb,
cc= A.cc,
dd= A.dd
from A,B where A.bb=@bb and A.aa=B.aa
if not exists(select * from B,A where B.AA=A.AA and A.bb=@bb)
insert into bb select * from a where a.bb=@bb
else
update b set bb=(select bb from a where a.bb=@bb),
cc=(select cc from a where a.bb=@bb),
dd=(select dd from a where a.bb=@bb)
declare @bb varchar(10)
if not exists(select * from a where a.aa=(select aa from b where b.bb=@bb))
insert into bb select * from a where a.bb=@bb
else
update b set bb=(select bb from a where a.bb=@bb),
cc=(select cc from a where a.bb=@bb),
dd=(select dd from a where a.bb=@bb)