使用 INNER JOIN 更新 SQL Server 中的多个表 [英] Update multiple tables in SQL Server using INNER JOIN

查看:39
本文介绍了使用 INNER JOIN 更新 SQL Server 中的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Server 并尝试使用 SQL 通过一个查询一次更新多个表:

I'm using SQL Server and trying to use SQL to update multiple tables at once with one query:

以下查询:

update table1
set A.ORG_NAME =  @ORG_NAME, B.REF_NAME = @REF_NAME
from table1 A, table2 B
where B.ORG_ID = A.ORG_ID
and A.ORG_ID = @ORG_ID

给出错误信息:

无法绑定多部分标识符A.ORG_NAME".

错误信息是什么意思?

推荐答案

你不能在一个语句中更新多个表,但是你得到的错误消息是因为别名,你可以试试这个:

You can't update more that one table in a single statement, however the error message you get is because of the aliases, you could try this :

BEGIN TRANSACTION

update A
set A.ORG_NAME =  @ORG_NAME
from table1 A inner join table2 B
on B.ORG_ID = A.ORG_ID
and A.ORG_ID = @ORG_ID

update B
set B.REF_NAME = @REF_NAME
from table2 B inner join table1 A
    on B.ORG_ID = A.ORG_ID
    and A.ORG_ID = @ORG_ID

COMMIT

这篇关于使用 INNER JOIN 更新 SQL Server 中的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆