Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a stored procedure to return the rowcount of another stored procedure

Is it possible to do this? I have some filters set in my source Stored Procedure and I really don't want to have to duplicate it in another just to get the rowcount.

like image 913
Mike Cole Avatar asked Dec 31 '22 02:12

Mike Cole


2 Answers

The only way I know how to do this is to insert into a temp table from the stored procedure and then select the count. Unfortunately, there's no pretty way to perform a "select" on a stored procedure.

CREATE TABLE #stuff (id int, status char(6))
INSERT #stuff (id, status)
EXEC dbo.sp_get_stuff

SELECT count(*) FROM #stuff

DROP TABLE #stuff

Edit

The above method will allow you to select from a stored procedure, but as Greg pointed out, a rowcount can be simplified to:

EXEC dbo.sp_get_stuff
SELECT @@Rowcount
like image 161
Gavin Miller Avatar answered Jan 13 '23 13:01

Gavin Miller


This also works:

create proc pTest1
as
select * from comp
go

create proc pTest2
as
exec pTest1
select @@rowcount
GO
like image 45
edosoft Avatar answered Jan 13 '23 13:01

edosoft