tags:

views:

2078

answers:

2

I am trying to update table A with data from table B. I thought I could do something like:

 UPDATE A
 SET A.name = B.name
 WHERE A.id = B.id

but alas, this does not work.

Anyone have an idea of how I can do this?

+10  A: 

Your query does not work because you have no FROM clause that specifies the tables you are aliasing via A/B.

Please try using the following:

UPDATE A
    SET A.NAME = B.NAME
FROM TableNameA A, TableNameB B
WHERE A.ID = B.ID

Personally I prefer to use more explicit join syntax for clarity i.e.

UPDATE A
    SET A.NAME = B.NAME
FROM TableNameA A
    INNER JOIN TableName B ON 
     A.ID = B.ID
John Sansom
+1 that's the way to do it!
marc_s
I don't know if you can have the alias for in the SET clause. Might depend on your flavor of SQL, but I believe that since it's a given that you're updating "A" that it's disallowed.
Tom H.
Thanks for ur answer...
Nahid
@Nahid: You're welcome, glad to help.
John Sansom
A: 

For Microsoft Access (don't laugh!)...

UPDATE TableA A
    INNER JOIN TableB B
    ON A.ID = B.ID
SET A.Name = B.Name
Nicolas