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 2005 Forums
 Transact-SQL (2005)
 sp help

Author  Topic 

ann
Posting Yak Master

220 Posts

Posted - 2009-09-30 : 18:25:20
I need to know if a certain id exists in either of 2 different tables:

TableA:
ID = 2
ID = 6
ID = 4

TableB:
ID = 4
ID = 19

Results expected if parameter 2 is used: true
Results expected if parameter 4 is used: true
Results expected if parameter 19 is used: true
Results expected if parameter 33 is used: false


I tried doing an inner join but get no results:

select TableA.ID, TableB.ID from TableA
Inner Join TableB on TableA.ID = TableB.ID

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-30 : 18:44:56
Just UNION them together and use a derived table.

SELECT *
FROM (SELECT ID FROM TableA UNION ALL SELECT ID FROM TableB) Table1
WHERE ID = @n

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-01 : 02:12:13
or apply to each table and union them

SELECT ID FROM TableA
WHERE ID = @n
UNION ALL
SELECT ID FROM TableB
WHERE ID = @n


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -