Allen King
Allen King

Reputation: 2516

Fastest way to select a record from another table if doesn't exist in the first table

Perhaps this is something very easy for SQL experts.

I have two tables:

Table A     TID    Desc
Table B     TID    Desc

I understand I can do IsExists but not sure if that is the fastest way. The requirement is that if the record exists in Table A for a given TID, the record should be read form Table A, else from table B. Record will definitely exist in Table B

Upvotes: 1

Views: 44

Answers (2)

paparazzo
paparazzo

Reputation: 45106

select isnull(A.val, B.val)   
  from B
  left join A
    on B.TID = A.TID

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93754

I don't think Exists will satisfy your requirement. Try using Left Outer Join

select Coalesce(A.somecol,B.somecol) 
from  tableB B 
left outer join tableA A
on B.TID = A.TID
Where B.TID = X  --Make sure you add the filter to TableB

Upvotes: 1

Related Questions