Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Is this possible retrieving single resultset from stored procdure that contains multiple select statements if yes than how
Posted
Comments
Prosan 29-May-12 7:50am    
in your all select statememnt is no of columns is same than you can use union
Prosan 29-May-12 8:20am    
is this query solve your problem. i hope it will return result as you want.

This might not be the right approach, but you will get result set as you mentioned

SQL
create proc usp_test
as
begin

declare @greater int, @lesser int
 SELECT @greater=COUNT([empId])FROM tbl_emp WHERE [age] > 12
 SELECT @lesser=COUNT([empId])FROM tbl_emp WHERE [age] < 12

SELECT @greater as Greaterthan12, @lesser as lessthan12

end
 
Share this answer
 
v2
Comments
Pratika05 29-May-12 8:14am    
thanx i got but does this way ever give me error oor wrong output
Ganga Patangi 29-May-12 8:21am    
Nope...you can use this...You wont get any errors or wrong output...
If you want to fetch data in like these:
SQL
Select count(*)as greater12 from tbl_emp where age>12
Select count(*)as less12 from tbl_emp where age<12

in the one result set, use UNION command[^].

SQL
SELECT 'Greater then 12' AS [Condition], COUNT([empId]) AS [HowMany]
FROM tbl_emp
WHERE [age] > 12
UNION ALL
SELECT 'Less then 12' AS [Condition], COUNT(empId) AS [HowMany]
FROM tbl_emp
WHERE [age] < 12


Result:
Condition         HowMany
Greater then 12   123
Less then 12      85


The second way is to use CASE[^]

SQL
 SELECT [Info], COUNT([Info]) AS [HowMany] 
FROM (SELECT [Info] =
    CASE
         WHEN [age] <= 12 THEN 'Less or equal 12'
         WHEN [age] >12 AND [age] < 30 THEN 'Greater then 12 and less then 30'
         WHEN [age] >= 30 AND [age] < 50 THEN 'Equal or greater then 30 and less then 50'
         WHEN [age] >= 50 THEN 'Equal or greater then 50'
         ELSE 'Error!'
      END
FROM tbl_emp) AS DT
GROUP BY DT.[Info]
ORDER BY DT.[HowMany]
 
Share this answer
 
v2
Comments
Sandeep Mewara 29-May-12 14:13pm    
5!
Maciej Los 29-May-12 14:33pm    
Thank you, Sandeep ;)
in your all select statememnt if no. of columns is same than you can use union it will return single resultset.
now you can write this query as i telling you

SQL
select (Select count(*)as greater12 from tbl_emp where age>12 ) + ' ' + (Select count(*)as less12 from tbl_emp where age<12 ) as Res
 
Share this answer
 
v3
Comments
Pratika05 29-May-12 7:55am    
But I need result in tabular Format Like I have query Select count(*)as greater12 from tbl_emp where age>12 Select count(*)as less12 from tbl_emp where age<12 Now I want this Type of result greater12 less12 34 2
Prosan 29-May-12 8:19am    
is this query solve your problem?
You can use union

SQL
create proc usp_example
as
begin
    select id,name from tbluser
    union
    select id,name from tblproduct 
end


In both select stmt should have same number of columns
 
Share this answer
 
v2
Comments
Pratika05 29-May-12 7:48am    
But I need result in tabular Format Like I have query
Select count(*)as greater12 from tbl_emp where age>12
Select count(*)as less12 from tbl_emp where age<12
Now I want this Type of result
greater12 less12
34 2

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