0

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.

Community
  • 1
  • 1
tmutton
  • 1,091
  • 7
  • 19
  • 42
  • 1
    You're giving aliasses to the tables, yet don't use them. Either you broke something when posting here or you'd never get the systax to work. I'm also not sure if it's even possible to update multiple tables in one statement, but this is one thing i can spot straight away atleast. – F.B. ten Kate Sep 02 '11 at 09:46
  • I'm giving fully qualified aliases on the sets because it wont pick up the aliases on the from. – tmutton Sep 02 '11 at 09:50
  • See my answer, i've made queries like that so often i lost count. However never tried updating multiple tables, could work straight away though... Not sure. – F.B. ten Kate Sep 02 '11 at 09:52

4 Answers4

3

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 
} 
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
2

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.

Community
  • 1
  • 1
F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
  • Thanks but it's still coming up with the error on o.Saved = The multi-part identifier "o.Saved" could not be bound. – tmutton Sep 02 '11 at 09:55
  • Ye, most likely can't do an update with a from clause when trying to do multiple tables. Try doing a variation on what Marco suggested. – F.B. ten Kate Sep 02 '11 at 09:57
1

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
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
Marco
  • 56,740
  • 14
  • 129
  • 152
  • Thanks Marco though it's throwing up syntax errors here. It's firstly throwing an error on cdb 'Expecting SET' which makes me believe that you cannot inner join before you SET. – tmutton Sep 02 '11 at 09:57
  • 1
    Personally i think it has something to do with USING, i don't know the USING statement personally (which doesn't mean to much though!). Mind you, i know USING dbname to specify what DB you want to run the query on, but i dont know it while joining. I edited a variation of Macro's answer in my post, using a "normal" on statement. – F.B. ten Kate Sep 02 '11 at 10:00
  • Is this for MySQL? I forgot to mention, I am using MS SQL. – tmutton Sep 02 '11 at 10:04
  • @F.B. ten Kate: I'm sorry, but it's the first time I try to use a multiple tables update and I don't know which the error could mean. I took a MySql example, maybe you're aven using a different db. Sorry again, I can't test queries at the moment... – Marco Sep 02 '11 at 10:04
  • I'm only talking about MS SQL Here, i dont work with MySQL so i can't say much there :) But since Codemonkey didn't specify your answer is perfectly valid. – F.B. ten Kate Sep 02 '11 at 10:05
  • F.B. is correct. You can't use `USING` here, the two fields in the JOIN have different names. – ypercubeᵀᴹ Sep 02 '11 at 10:06
  • @codemonkey: Are you using SQL-Server (which version?) or Ms-Access? – ypercubeᵀᴹ Sep 02 '11 at 10:08
  • 2
    You can't update 2 tables in one statement in SQL-Server: See this answer for a solution: http://stackoverflow.com/questions/2044467/how-to-update-two-tables-in-one-statement-in-sql-server-2005/2044520#2044520 – ypercubeᵀᴹ Sep 02 '11 at 10:11
  • 1
    Aaawh! i just linked that aswell :( But ye, impossible sadly. – F.B. ten Kate Sep 02 '11 at 10:13
  • Moreover..this goes same as constraints appies to updating a View – Shantanu Gupta Sep 06 '11 at 06:16
0

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.

Hamikzo
  • 254
  • 1
  • 9