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
 add data to where clause

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-06-06 : 10:21:25
Hi there,

I need to grab some items into a where clause, see below:
table1
ID | Location
10| New York
11| Dallas
...

select * from table2 where Location = 'New York' or Location = 'Dallas' ...


How can I select Location from table1 to where clause in table2?
Thanks in advance.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-06 : 10:23:47
select * from table2 where location in (select distinct location from table1)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-06 : 10:23:48
select * from table2 where Location in (select location frrom table)

or
select t2.*
from table2 t2
join table1 t1
on t1.location = t2.location

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-06-06 : 10:31:31
Thanks webfred and nigelrivett, I never thought it's this simple! Thanks again.
Go to Top of Page
   

- Advertisement -