简体   繁体   中英

Counting results of stored procedure

I have a stored procedure returning ID, Name, Descriptions and takes no input parameters. However, I am interested in how many results do I get.

I expected something like this work:

SELECT COUNT(*) FROM EXEC MyStoredProcedure

But I get the following error in SqlServer Managment Studio: Incorrect syntax near the keyword 'EXEC'. Could you show me a little code example how can I do that?

This won't work. May I suggest:

exec MyStoredProcedure
select @@rowcount

Alternatively you could return the count as an output parameter

You need to put the logic in the stored proc and return the count from the stored proc. You do this by using the @@ROWCOUNT variable immediately after your query. This ihow it would work in MS SQL Servet at least.

Stored Proc:

CREATE PROC MyPROC
AS
DECLARE @MyCount int

...

SELECT * FROM MyTable WHERE ...

SELECT @MyCount = @@ROWCOUNT

...

return @MyCOunt

Calling code:

DECLARE @MyCount int

EXEC @MyCount = EXEC MyProc
SELECT @@ROWCOUNT

编写一个新的存储过程,为您计算。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM