注册 登录
编程论坛 MySQL论坛

[求助]求助:存储过程写法

myquestion 发布于 2006-05-08 23:19, 1482 次点击

1. 假设有以下的两个表:

Cus_A

ID*

Name

Address

Cus_B

ID*

Name

Address

*主键

表Cus_A和表Cus_B的结构完全相同,表Cus_A和表Cus_B中既存在ID相同的记录,也存在ID不同的记录。现要求将ID只存在于表Cus_A中而不存在于表Cus_B中的记录全部插入到Cus_B表中,并用表Cus_A中的记录更新表Cus_B中相同的ID的记录,请写出完成这一功能的存储过程。

1 回复
#2
jimn20002006-07-07 22:39

create procedure dd
as
begin

select * into #temp from b where 1<>1

insert into #temp
select * from b
union all
select * from a
where a.id not in
(select id from b )
update #temp
set #temp.name=a.name
#temp.address=a.address
where #temp.id=a.id
select * from #temp
end

1