Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help in Update statement

Author  Topic 

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2013-02-06 : 12:50:21
I need to update statusB column in tableA by comparing server name.
If the server name in tableA is matching with Server name in tableB then the staus column in tableA should be Yes else No

AS IS

table A statusB TableB
----------------- ---------
Server1 Server1
server2 server8
server3 server6
server4 server4


TO BE

Table A StatusB
--------------------
Server1 Yes
server2 No
server3 No
Server4 yes

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-06 : 13:08:15
Do you have two tables or just one? If you have two tables (and if the columns are what I am guessing they are), then may be this is what you need:
UPDATE a SET
[statusB] = CASE WHEN b.ServerName IS NULL THEN 'No' ELSE 'Yes' END
FROM
TableA a
LEFT JOIN TableB b ON a.Servername = b.ServerName;
If it is all in one table and you are replacing the second column with Yes or No, then
UPDATE TableA SET
TableB = CASE WHEN statusB = TableB THEN 'Yes' ELSE 'No' END;
Go to Top of Page

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2013-02-06 : 13:39:08
Thank you very much for your prompt reply...
Go to Top of Page
   

- Advertisement -