SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to embed the var in where clause?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 11/11/2012 :  22:59:54  Show Profile  Reply with Quote
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
2202 Posts

Posted - 11/11/2012 :  23:17:28  Show Profile  Reply with Quote

DECLARE @xyz int
SET @xyz = YourValue ------------eg: 20

select * from table1
where code in (select code from table2
where model = @xyz)



--
Chandu
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17584 Posts

Posted - 11/11/2012 :  23:47:43  Show Profile  Reply with Quote
refer to http://www.sommarskog.se/arrays-in-sql.html


KH
Time is always against us

Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 11/12/2012 :  14:01:54  Show Profile  Reply with Quote
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.

Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17584 Posts

Posted - 11/12/2012 :  19:56:08  Show Profile  Reply with Quote
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

Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 11/13/2012 :  11:08:55  Show Profile  Reply with Quote
Yes, it works that way. Thanks guys!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000