Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
declare @dist_No int
set @dist_no= select dist_No from megamart where sponsor_ID_No=11111
print @dist_no


in case my query is returning one result(i.e. @dist_no=1) it works fine and i can use query's result by using @dist_no

but what if my query is returing two results
1
2
then how can i store them
and how can i use them
for instance i want to use 1 in some queries and 2 in another queries
Posted
Comments
Ashish_Agrawal 19-Nov-13 5:50am    
what are your next queries? can you give us some example where you are going to use @dist_no.

 
Share this answer
 
Comments
prashantttt 19-Nov-13 5:06am    
yess... I got your point you want me to store output in table variable and use it when needed....
but,
select Dist_no from megamart where Sponsor_No =1
is resulting me two results and I want to use these results for next queries....
i.e. I will b looping some queries based on Dist_No returned by above query....
So, plz tell me How can I use this output for my queries....
Mehdi Gholam 19-Nov-13 5:53am    
You can use a cursor : http://technet.microsoft.com/en-us/library/ms180169.aspx
As far I know I don't think its possible. Alternative is use a table variable and store the values in it. You have mentioned you want to use them but not sure if you want to use them in SQL only if so then this solution should work.
 
Share this answer
 
Comments
prashantttt 19-Nov-13 4:02am    
I will use them in sql only
ArunRajendra 19-Nov-13 5:16am    
Ok. The solution should work.
prashantttt 19-Nov-13 5:23am    
Not workking actuallly... :(
I'm confused

plz check comments for solution 1....
ArunRajendra 19-Nov-13 5:27am    
It will be better if you could share more details about your other queries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900