是否可以为两个表创建更新命令 [英] Is it possible to have an update command for two tables
本文介绍了是否可以为两个表创建更新命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以加入两个表并有一个更新命令来更新这两个表吗?
谢谢
Can we join two tables and have an update command which updates both the tables?
Thanks
推荐答案
你不能在一个语句中更新多个表。而不是你可以使用事务来确保原子地处理两个更新语句。
例如:
You can''t update multiple tables in one statement. Instead of that you can use a transaction to make sure that two Update statements are treated atomically.
Eg:
DECLARE @MyTable TABLE (id int);
BEGIN TRANSACTION
UPDATE Table1
SET Table1.Name = 'Test'
OUTPUT INSERTED.id INTO @MyTable
WHERE T1.field = 'Your Value';
UPDATE Table2
SET Table2.YourColumnName = 'Your Value'
FROM Table2
JOIN @MyTable m on m.id = Table2.id;
COMMIT;
--Amit
--Amit
这篇关于是否可以为两个表创建更新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文