简体   繁体   English

计算存储过程的结果

[英]Counting results of stored procedure

I have a stored procedure returning ID, Name, Descriptions and takes no input parameters. 我有一个存储过程返回ID,名称,描述,并没有输入参数。 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'. 但是我在SqlServer Managment Studio中收到以下错误:关键字'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. 您可以在查询后立即使用@@ ROWCOUNT变量来执行此操作。 This ihow it would work in MS SQL Servet at least. 这样它至少可以在MS SQL Servet中使用。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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