views:

420

answers:

3

this is my query in sql server 2008 -

    UPDATE a 
       SET a.col2 = 'new', 
           a.col3 = 'www.google.com', 
           b.col1 = '10'
      FROM table a 
INNER JOIN table b ON a.col1 = b.col1 
     WHERE a.col1 = 7

it crashes stating "Invalid column name b.col1."

How do i make this work?

+1  A: 

From looking at your query a little closer, you have b.Col1 in the UPDATE statement. this is incorrect

UPDATE  a
SET a.col2='new', 
        a.col3='www.google.com', 
        b.col1='10' 
FROM    @table a INNER JOIN 
        @table b ON a.col1 = b.col1 
WHERE   a.col1=7

From UPDATE you can only update 1 table at a time

astander
you can't update table a and table b with 1 update statement
SQLMenace
Yes, I think that is what I said. *From UPDATE you can only update 1 table at a time*
astander
oops..didn't see that part
SQLMenace
+1  A: 

You can only update 1 table at a time

you need to issue 2 update statements

UPDATE a SET a.col2='new', a.col3='www.google.com'
FROM tablea a INNER JOIN tableb  b ON a.col1 = b.col1
WHERE a.col1=7

UPDATE b SET b.col1='10' 
FROM tablea a INNER JOIN tableb b ON a.col1 = b.col1
WHERE a.col1=7
SQLMenace
A: 

Your statement is "Update A", and you're trying to update a column in table B. You might want to create a view containing the columns in tables A and B, and update that. You could also create triggers for table A - perhaps one that will update the appropriate jointed column in table B.

Liz Albin
even with a view you can only update 1 base table at a time..unless you add a trigger to the view
SQLMenace