SQL 存储过程的计数结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting results of stored procedure
提问by GarbageGuy
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.
我有一个存储过程返回 ID、名称、描述并且不接受任何输入参数。但是,我对获得多少结果感兴趣。
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?
但是我在 SqlServer Managment Studio 中收到以下错误:关键字“EXEC”附近的语法不正确。你能告诉我一个小代码示例我该怎么做?
回答by Chris Simpson
This won't work. May I suggest:
这行不通。我可以建议:
exec MyStoredProcedure
select @@rowcount
Alternatively you could return the count as an output parameter
或者,您可以将计数作为输出参数返回
回答by Charles Graham
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.
您需要将逻辑放在存储过程中并从存储过程中返回计数。您可以在查询后立即使用@@ROWCOUNT 变量来完成此操作。这至少可以在 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
回答by Quassnoi
SELECT @@ROWCOUNT
回答by tehvan
Write a new stored procedure which does a count for you.
编写一个新的存储过程来为您计数。