dbTalk Databases Forums  

Updating a column in a table from a temp table

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Updating a column in a table from a temp table in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
amj1020
 
Posts: n/a

Default Updating a column in a table from a temp table - 11-11-2009 , 10:05 AM






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.

Reply With Quote
  #2  
Old   
Pindle
 
Posts: n/a

Default Re: Updating a column in a table from a temp table - 11-11-2009 , 10:53 AM






On 11 Nov, 16:05, amj1020 <angelicre... (AT) hotmail (DOT) com> wrote:
Quote:
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.
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.

Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Updating a column in a table from a temp table - 11-11-2009 , 12:17 PM



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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.