在一条语句中更新 2 个表 (MS SQL)
我有一条更新语句。它更新一个字段,但内部联接另一表以完成 where 子句。我正在使用 MS SQL。
我现在尝试更新连接表上的字段,但似乎无法做到这一点。我读到一次只能更新一张表。这是真的吗?有什么办法可以解决这个问题吗?
这是我的声明
update tbl_calendardatebox
set
tbl_calendardatebox.HeaderBgColour = @value,
tbl_calendarobjects.Saved = '0'
from tbl_calendardatebox db
inner join tbl_calendarobjects o on
db.ObjectId = o.Id
where o.PageId = @page
and o.GroupField = @group and o.GroupField <> '-1'
and o.Visible = '1'
and o.CanUserEdit = '1'
and db.HeaderBgColour <> @value
所以这两个表是tbl_calendardatebox和tbl_calendarobjects。我在 tbl_calendarobjects.Saved = '0' 上收到错误消息 - 无法绑定多部分标识符“tbl_calendarobjects.Saved”。
任何帮助将不胜感激。
Possible Duplicate:
How to update two tables in one statement in SQL Server 2005?
I have an update statement. It Updates one field but inner joins on another table to complete the where clause. I am using MS SQL.
I am now trying to update a field on the joined table but cannot seem to do it. I have read that you can only update one table at a time. Is this true? Is there a way I can get around this?
Here is my statement
update tbl_calendardatebox
set
tbl_calendardatebox.HeaderBgColour = @value,
tbl_calendarobjects.Saved = '0'
from tbl_calendardatebox db
inner join tbl_calendarobjects o on
db.ObjectId = o.Id
where o.PageId = @page
and o.GroupField = @group and o.GroupField <> '-1'
and o.Visible = '1'
and o.CanUserEdit = '1'
and db.HeaderBgColour <> @value
So the two tables are tbl_calendardatebox and tbl_calendarobjects. I get the error message on tbl_calendarobjects.Saved = '0' - The multi-part identifier "tbl_calendarobjects.Saved" could not be bound.
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
同时更新多个表是不可能的。以下是 MSDN 的摘要清楚地显示
{} 代表必填字段
[] 代表可选字段
[...n] 代表 0 个或多个
查看此内容
以下部分不能包含任何连接。 “{ }”表示必填字段,可以包含视图名称或表名称,但不能包含联接。
Updating multiple tables at the same time is not possible. Following is the abstract from MSDN which clearly shows
{} represents mandatory field
[] represents optional fields
[...n] represents 0 or more
See this
Following section cannot contain any join. "{ }" represents mandatory field which can contain View Name or table name but not join.
首先修复语法,也可能解决问题,但未经测试!
在 stackoverflow 上找到的实际答案:如何在 SQL Server 2005 中用一条语句更新两个表?
遗憾的是,这是不可能的。
Syntax fix to begin with, might solve the problem as well, untested!
Actual answer found on stackoverflow: How to update two tables in one statement in SQL Server 2005?
This is NOT possible Sadly.
希望这个例子(取自这里)可以帮助你:
您查询(我无法测试,抱歉)可能是:
Hope this example (taken from here) can help you:
You query (I cannot test it, sorry) could be:
正如 Shantanu 所说,更新多个表是不可能的。
不管怎样,如果你的环境允许,你可以走程序路线。
它是安全且原子的。请记住,在捕获错误时,始终回滚。
Like Shantanu told, it isn't possible to update multiple tables.
Anyway, if your envirornment allow it, you could walk on procedure way.
It's safe and atomic. Remember, on catch an error, rollback always.