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.
Author |
Topic |
ann
Posting Yak Master
220 Posts |
Posted - 2007-03-05 : 09:20:37
|
I need to find if a value exists in another table so that I will know how to handle it within an application:TableA:AID, Title, Desc1 tit1 Desc12 ti2 Desc23 Ti3 Desc34 T14 desc4 TableB:BID AID Comment1 1 blah blah2 3 mmmmmResult:AID Title Desc InUse1 tit1 Desc1 12 ti2 Desc2 03 Ti3 Desc3 14 T14 desc4 0If anyone can help, I would appreciate it - thank you |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-05 : 09:26:58
|
[code]declare @TableA table( AID int, Title varchar(10), [Desc] varchar(10))insert into @TableAselect 1, 'tit1', 'Desc1' union allselect 2, 'ti2', 'Desc2' union allselect 3, 'Ti3', 'Desc3' union allselect 4, 'T14', 'desc4'declare @TableB table( BID int, AID int, Comment varchar(10))insert into @TableBselect 1, 1, 'blah blah' union allselect 2, 3, 'mmmmm'select a.*, InUse = case when b.AID is null then 0 else 1 endfrom @TableA a left join @TableB b on a.AID = b.AID/*AID Title Desc InUse ----------- ---------- ---------- ----------- 1 tit1 Desc1 12 ti2 Desc2 03 Ti3 Desc3 14 T14 desc4 0*/[/code] KH |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-05 : 09:30:43
|
[code]Select t1.AID, t1.Title, t1.Desc, Case t2.AID Is Not NULL then 1 else 0 end as InUseFrom TableA t1 LEFT JOIN TableB t2on t1.AID = t2.AID[/code] Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-03-05 : 09:32:13
|
That was quick! Thanks that is exactly what I need.I appreciate your time. |
 |
|
|
|
|
|
|