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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 check key exist

Author  Topic 

nirnir
Starting Member

10 Posts

Posted - 2013-10-16 : 05:53:39
I have two tables

TableA
key :char[20]
data:char[100]

TableB
kay :char[20]
existInTableA : bit

I need to update existInTableA field in each record of tableB
to indicate if tableA contains record with same key
what is the best way to do that ?

attached sample data

TableA
-----------
key1,data1
key2,data2
key4,data4

TableB
----------
key1,true
key2,true
key3,false
key4,true



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-16 : 08:01:27
[code]
UPDATE b
SET existInTableA = 1
FROM TableB b
WHERE EXISTS (SELECT 1 FROM TableA WHERE key = b.key)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -