| Author |
Topic  |
|
|
allan8964
Posting Yak Master
196 Posts |
Posted - 11/11/2012 : 22:59:54
|
Hi there,
I have a table1 need some variables, could be 3 or 20, in where clause like this:
select * from table1 where code in ('abc', 'bbc', 'cbs')
I need provide the items of 'abc' from table2, like select code from table2 where model = @xyz
I can use a temporary table to get all code records from table2 but the problem is that with different @xyz the code number could be 3 or 20. Then how can I decide to embed these var in the IN statement? Thanks in advance.
|
|
|
bandi
Flowing Fount of Yak Knowledge
India
1430 Posts |
Posted - 11/11/2012 : 23:17:28
|
DECLARE @xyz int SET @xyz = YourValue ------------eg: 20
select * from table1 where code in (select code from table2 where model = @xyz)
-- Chandu |
 |
|
|
khtan
In (Som, Ni, Yak)
Singapore
16746 Posts |
|
|
allan8964
Posting Yak Master
196 Posts |
Posted - 11/12/2012 : 14:01:54
|
Thanks guys. I think it's my fault not describing my requirement clear. When I say 3 or 20 I didn't mean the value is 3 or 20. Actaully I meant there are 3 or 20 items inside the IN (), like
where code in ('abc', 'bbc', 'cbs' .... ) there could be 3 to 20 of those 'abc', 'bbc' ...
If col CODE from table2 returns 3 values then I can use a temporary table to feed the IN() but what if table2 returns 20 values? Thanks.
|
 |
|
|
khtan
In (Som, Ni, Yak)
Singapore
16746 Posts |
Posted - 11/12/2012 : 19:56:08
|
quote: Originally posted by allan8964
Thanks guys. I think it's my fault not describing my requirement clear. When I say 3 or 20 I didn't mean the value is 3 or 20. Actaully I meant there are 3 or 20 items inside the IN (), like
where code in ('abc', 'bbc', 'cbs' .... ) there could be 3 to 20 of those 'abc', 'bbc' ...
If col CODE from table2 returns 3 values then I can use a temporary table to feed the IN() but what if table2 returns 20 values? Thanks.
You can still use the query posted by bandi for that. You don't need a temp table to do that
KH Time is always against us
|
 |
|
|
allan8964
Posting Yak Master
196 Posts |
Posted - 11/13/2012 : 11:08:55
|
| Yes, it works that way. Thanks guys! |
 |
|
| |
Topic  |
|