Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updating a column in a table from a temp table

0 views
Skip to first unread message

amj1020

unread,
Nov 11, 2009, 11:05:02 AM11/11/09
to
How do I update a new column in a table with data from a temp table

Table with added field errormsg
Table1
Name char(50)
clientkey varchar(50)
ErrorMsg varchar(50)

Temp table
Clientkey varchar(50)
ErroMsg varchar(50)

Right now in table1 the ErrorMsg field is null for every Name.

Pindle

unread,
Nov 11, 2009, 11:53:22 AM11/11/09
to

Try the following, it will set the ErrorMsg values in Table1 to be
equal to the ErrorMsg values in the temp table.

UPDATE Table1
SET Table1.ErrorMsg = TempTable.ErrorMsg
WHERE Table1.ClientKey = TempTable.ClientKey

Thanks.

Plamen Ratchev

unread,
Nov 11, 2009, 1:17:23 PM11/11/09
to
You can use the ANSI update syntax:

UPDATE Table1
SET ErrorMsg = (SELECT T.ErrorMsg
FROM Temp AS T
WHERE T.clientkey = Table1.clientkey)
WHERE EXISTS(SELECT *
FROM Temp AS T
WHERE T.clientkey = Table1.clientkey);

Or the SQL Server specific update with join (note here that multiple matches on clientkey will result in
non-deterministic update):

UPDATE Table1
SET ErrorMsg = T.ErrorMsg
FROM Table1 AS A
JOIN Temp AS T
ON A.clientkey = T.clientkey;

--
Plamen Ratchev
http://www.SQLStudio.com

0 new messages