如何更新多表中的字段?(在线的等候)

liq1979 2003-08-04 07:22:25
例如: 我有三张表 Table1(A1,B1,C1),Table2(A2,B2,C2),Table3(A3,B3,C3) 之间的关系为Table1.A1=Table2.A2;Table1.B1=Table3.A3 想用update的方法更新三张表中的数据。
我是这样写的:
Update Table1
Set A1='xxx',B1='xxx',C1='xxx',B2='xxx',C2='xxx',B3='xxx',C3='xxx'
From Table1 INNER JOIN Table2
on Table1.A1= Table2.A2
INNER JOIN Table3
on Table1.B1= Table3.A3

请问如何写才能把B2,C2,B3和C3的数据保存到数据库中!
请教各位高手了!我在线等候!回答满意的,给100分,谢谢!
...全文
22 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-08-04
  • 打赏
  • 举报
回复
一个更新语句只能更新一个表,要用三条更新语句:

begin tran --利用事务处理,保证三张表都会被更新
update Table1 Set A1='xxx',B1='xxx',C1='xxx'
update Table2 Set B2='xxx',C2='xxx'
update Table3 Set B3='xxx',C3='xxx'
commit tran
pengdali 2003-08-04
  • 打赏
  • 举报
回复
create view 视图
as
select Table1.A1,Table1.B1,Table1.C1,Table2.A2,Table2.B2,Table2.C2,Table3.A3,Table3.B3,Table3.C3 from table1,table2,table3 where Table1.A1=Table2.A2;Table1.B1=Table3.A3
go

CREATE TRIGGER 名 on 视图
INSTEAD OF update
AS
BEGIN
update table1 set c1=inserted.c1 from inserted where inserted.a1=table1.a1 and inserted.b1=table1.b1
update table2 set b2=inserted.b2,c2=inserted.c2 from inserted where inserted.a2=table1.a2
update table3 set a3=inserted.a3,c3=inserted.c3 from inserted where inserted.b3=table1.b3
END
go

update 视图 set C1='xxx',B2='xxx',C2='xxx',A3='xxx',C3='xxx' where a1='xx' and b1='yy'
txlicenhe 2003-08-04
  • 打赏
  • 举报
回复
Update Table1
Set A1='xxx',B1='xxx',C1='xxx'

Update Table2 ...
Update Table3 ...

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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