在一条语句中更新 2 个表 (MS SQL)

发布于 2024-12-02 18:22:08 字数 948 浏览 7 评论 0原文

可能的重复:
如何在一条语句中更新两个表在 SQL Server 2005 中?

我有一条更新语句。它更新一个字段,但内部联接另一表以完成 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_calendardateboxtbl_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴意少年 2024-12-09 18:22:08

同时更新多个表是不可能的。以下是 MSDN 的摘要清楚地显示

{} 代表必填字段

[] 代表可选字段

[...n] 代表 0 个或多个

查看此内容

UPDATE 
        { 
         table_name WITH ( < table_hint_limited > [ ...n ] ) 
         | view_name 
         | rowset_function_limited 
        } 
        SET 
        { column_name = { expression | DEFAULT | NULL } 
        | @variable = expression 
        | @variable = column = expression } [ ,...n ] 

    { { [ FROM { < table_source > } [ ,...n ] ] 

        [ WHERE 
            < search_condition > ] } 
        | 
        [ WHERE CURRENT OF 
        { { [ GLOBAL ] cursor_name } | cursor_variable_name } 
        ] } 
        [ OPTION ( < query_hint > [ ,...n ] ) ] 

以下部分不能包含任何连接。 “{ }”表示必填字段,可以包含视图名称或表名称,但不能包含联接。

{ 
 table_name WITH ( < table_hint_limited > [ ...n ] ) 
 | view_name 
 | rowset_function_limited 
} 

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

UPDATE 
        { 
         table_name WITH ( < table_hint_limited > [ ...n ] ) 
         | view_name 
         | rowset_function_limited 
        } 
        SET 
        { column_name = { expression | DEFAULT | NULL } 
        | @variable = expression 
        | @variable = column = expression } [ ,...n ] 

    { { [ FROM { < table_source > } [ ,...n ] ] 

        [ WHERE 
            < search_condition > ] } 
        | 
        [ WHERE CURRENT OF 
        { { [ GLOBAL ] cursor_name } | cursor_variable_name } 
        ] } 
        [ OPTION ( < query_hint > [ ,...n ] ) ] 

Following section cannot contain any join. "{ }" represents mandatory field which can contain View Name or table name but not join.

{ 
 table_name WITH ( < table_hint_limited > [ ...n ] ) 
 | view_name 
 | rowset_function_limited 
} 
半夏半凉 2024-12-09 18:22:08

首先修复语法,也可能解决问题,但未经测试!

update db
set 
db.HeaderBgColour = @value,
o.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

在 stackoverflow 上找到的实际答案:如何在 SQL Server 2005 中用一条语句更新两个表?

遗憾的是,这是不可能的。

Syntax fix to begin with, might solve the problem as well, untested!

update db
set 
db.HeaderBgColour = @value,
o.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

Actual answer found on stackoverflow: How to update two tables in one statement in SQL Server 2005?

This is NOT possible Sadly.

情释 2024-12-09 18:22:08

希望这个例子(取自这里)可以帮助你:

UPDATE a 
INNER JOIN b USING (id) 
SET a.firstname='Pekka', a.lastname='Kuronen', 
b.companyname='Suomi Oy',companyaddress='Mannerheimtie 123, Helsinki Suomi' 
WHERE a.id=1; 

您查询(我无法测试,抱歉)可能是:

UPDATE tbl_calendardatebox cdb
  INNER JOIN tbl_calendarobjects co
    ON cdb.ObjectId = co.Id
SET cdb.HeaderBgColour = @value
  , co.Saved = '0'
WHERE co.PageId = @page
AND co.GroupField = @group 
AND co.GroupField <> '-1'
AND co.Visible = '1'
AND co.CanUserEdit = '1'
AND cdb.HeaderBgColour <> @value

Hope this example (taken from here) can help you:

UPDATE a 
INNER JOIN b USING (id) 
SET a.firstname='Pekka', a.lastname='Kuronen', 
b.companyname='Suomi Oy',companyaddress='Mannerheimtie 123, Helsinki Suomi' 
WHERE a.id=1; 

You query (I cannot test it, sorry) could be:

UPDATE tbl_calendardatebox cdb
  INNER JOIN tbl_calendarobjects co
    ON cdb.ObjectId = co.Id
SET cdb.HeaderBgColour = @value
  , co.Saved = '0'
WHERE co.PageId = @page
AND co.GroupField = @group 
AND co.GroupField <> '-1'
AND co.Visible = '1'
AND co.CanUserEdit = '1'
AND cdb.HeaderBgColour <> @value
相对绾红妆 2024-12-09 18:22:08

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文