Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get COUNT from stored procedure return?

I have a stored procedure in SQL which I can't change. It needs few input parameters and returns a table with 100+ rows and several columns.

exec dbo.Select_Data 0, 0, 18, 50

I need something to get count of returned rows:

select count(*) from (exec dbo.Select_Data 0, 0, 18, 50)

and a way to get values from e.g. Name column:

select Id, Name from (exec dbo.Select_Data 0, 0, 18, 50) where Id=10

How do I do this?

like image 978
Kromster Avatar asked Sep 17 '10 08:09

Kromster


1 Answers

You need to create a temp table to hold the results of the stored procedure. you can then query the temp table. The schema of the temp table must match the output of the stored procedure.

Example:

CREATE TABLE #temp
(
ID INT,
NAME VARCHAR(100),
...
)

INSERT INTO #temp
Exec dbo.MyStoredProc

SELECT COUNT(*) FROM #temp

SELECT ID, NAME FROM #temp 
WHERE ID = 10

DROP TABLE #temp
like image 99
codingbadger Avatar answered Nov 03 '22 00:11

codingbadger