高手请进,sql存储过程笔试题目

ziyan0621 2009-09-15 11:51:41


假设有以下的两个表:
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的记录,请写出完成这一功能的存储过程。
...全文
268 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_wsx 2009-09-15
  • 打赏
  • 举报
回复
好快呀!
gsk09 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 guguda2008 的回复:]
SQL codeCREATEPROCEDURE TEST()ASBEGINDELETEFROM Cus_BWHEREEXISTS(SELECT1FROM Cus_AWHERE Cus_B .ID=Cus_A .ID)INSERTINTO CUS_BSELECT*FROM CUS_AEND
[/Quote]


CREATE PROCEDURE TEST()
AS
BEGIN

DELETE Cus_B
FROM Cus_A
WHERE Cus_B.ID=Cus_A.ID

INSERT INTO CUS_B
SELECT * FROM CUS_A

END
sgtzzc 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 beirut 的回复:]
引用 15 楼 guguda2008 的回复:
引用 10 楼 sgtzzc 的回复:
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago

不能用TRUNCATE,两个表各有对方不存在的ID

不是两个吗? 楼主不是要先插入后更新的
[/Quote]

而且更新的话,如果字段不知道有多少个,就不好写
sgtzzc 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 guguda2008 的回复:]
引用 19 楼 beirut 的回复:
引用 15 楼 guguda2008 的回复:
引用 10 楼 sgtzzc 的回复:
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago

不能用TRUNCATE,两个表各有对方不存在的ID

不是两个吗? 楼主不是要先插入后更新的

如果A是123,B是234,TRUNCATE就把1删了,再插234就掉数据了
[/Quote]

看到了,所以改成了18楼
sgtzzc 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 beirut 的回复:]
引用 15 楼 guguda2008 的回复:
引用 10 楼 sgtzzc 的回复:
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago

不能用TRUNCATE,两个表各有对方不存在的ID

不是两个吗? 楼主不是要先插入后更新的
[/Quote]

删除存在的,再插入,和更新就是一回事
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 beirut 的回复:]
引用 15 楼 guguda2008 的回复:
引用 10 楼 sgtzzc 的回复:
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago

不能用TRUNCATE,两个表各有对方不存在的ID

不是两个吗? 楼主不是要先插入后更新的
[/Quote]
如果A是123,B是234,TRUNCATE就把1删了,再插234就掉数据了
黄_瓜 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 guguda2008 的回复:]
引用 10 楼 sgtzzc 的回复:
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago

不能用TRUNCATE,两个表各有对方不存在的ID
[/Quote]
不是两个吗? 楼主不是要先插入后更新
sgtzzc 2009-09-15
  • 打赏
  • 举报
回复
create proc sp_test
as
--删除Cus_B表与Cus_A表id相同的记录
delete Cus_B where exists(select 1 from Cus_A where id=Cus_B.id);
--插入Cus_A表中的记录到Cus_B表中
insert Cus_B select * from Cus_A;
go
黄_瓜 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 beirut 的回复:]
SQL code--> 测试数据:[Cus_A]ifobject_id('[Cus_A]')isnotnulldroptable[Cus_A]gocreatetable[Cus_A]([ID]int,[Name]varchar(4),[Address]varchar(4))insert[Cus_A]select1,'张三','河南'unionallselect2,'李四','洛阳'unionall¡­
[/Quote]

16楼?
黄_瓜 2009-09-15
  • 打赏
  • 举报
回复
--> 测试数据:[Cus_A]
if object_id('[Cus_A]') is not null drop table [Cus_A]
go
create table [Cus_A]([ID] int,[Name] varchar(4),[Address] varchar(4))
insert [Cus_A]
select 1,'张三','河南' union all
select 2,'李四','洛阳' union all
select 3,'王五','北京' union all
select 4,'顺六','武汉' union all
select 5,'田七','上海'

select * from [Cus_A]
--> 测试数据:[Cus_B]
if object_id('[Cus_B]') is not null drop table [Cus_B]
go
create table [Cus_B]([ID] int,[Name] varchar(4),[Address] varchar(4))
insert [Cus_B]
select 1,'张三','河南' union all
select 2,'李四','洛阳' union all
select 3,'王五','北京' union all
select 6,'小二','重庆' union all
select 7,'王八','厦门'
-------------查询开始----------------
create proc pr_test
as
begin
insert into cus_b
select *
from cus_a a
where not exists(select 1 from cus_b where a.id = id)

update Cus_B
set name=a.name,address=a.address
from Cus_B b,Cus_A a
where a.id=b.id


end
go
exec pr_test
select * from [Cus_B]
----------------结果---------
/*
ID Name Address
----------- ---- -------
1 张三 河南
2 李四 洛阳
3 王五 北京
6 小二 重庆
7 王八 厦门
4 顺六 武汉
5 田七 上海

(7 行受影响)


*/
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sgtzzc 的回复:]
SQL codecreateproc sp_testastruncatetable Cus_B ;insert Cus_Bselect*from Cus_Ago
[/Quote]
不能用TRUNCATE,两个表各有对方不存在的ID
华夏小卒 2009-09-15
  • 打赏
  • 举报
回复

create proc pp
as
update Cus_B
set name=a.name,address=a.address
from Cus_B b,Cus_A a
where a.id=b.id
---可以先更新,再插入
insert Cus_B select * from Cus_A where id not in (select id from Cus_B)


--小F-- 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 guguda2008 的回复:]
晕,你们看题啊,不只是插入还有更新呢
[/Quote]

又错 伤心
--小F-- 2009-09-15
  • 打赏
  • 举报
回复
汗  想错了
create proc f as
begin
insert into cus_b
select *
from cus_a a
where not exists(select 1 from cus_b where a.id = id)
end
go
-狙击手- 2009-09-15
  • 打赏
  • 举报
回复
create proc pr_test
as
begin
update b --还要更新,没注意到
set .....
from cus_b,cus_a
where a.id = b.id
insert into cus_b
select *
from cus_a a
where not exists(select 1 from cus_b where a.id = id)
end
go
sgtzzc 2009-09-15
  • 打赏
  • 举报
回复
create proc sp_test
as
truncate table Cus_B ;
insert Cus_B select * from Cus_A
go
ks_reny 2009-09-15
  • 打赏
  • 举报
回复

create proc test
as
begin
delete cus_b where id in(select id from cus_a)
insert into cus_b
select * from cus_a
end
soft_wsx 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyflystone 的回复:]
SQL codecreateproc pr_testasbegininsertinto cus_bselect*from cus_a awherenotexists(select1from cus_bwhere a.id= id)endgo
[/Quote]支持!
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
晕,你们看题啊,不只是插入还有更新呢
华夏小卒 2009-09-15
  • 打赏
  • 举报
回复

create proc pp
as
insert Cus_B select * from Cus_A where id not in (select id from Cus_B)

update Cus_B
set name=a.name,address=a.address
from Cus_B b,Cus_A a
where a.id=b.id
加载更多回复(11)

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧