SQL 存储过程选择到一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:31:50  来源:igfitidea点击:

Store Procedure Select into a variable

sqlsql-serverstored-procedures

提问by iosdevnyc

How can I count the result of a table and pass into a Stored Procedure Variable?

如何计算表的结果并将其传递给存储过程变量?

DECLARE @totalrecs varchar
select count(id) from table1

I want the count record in the totalrecs variable.

我想要 totalrecs 变量中的计数记录。

回答by SQLMenace

like this

像这样

--will not count NULLS
select @totalrecs= count(id) from table1

--will count NULLS
select @totalrecs= count(*) from table1

回答by Leigh S


DECLARE @totalCount Int
Select @totalCount = count(*) 
From table1

Exec sp_DoSomething @Var = @totalCount

回答by Rockcoder

select @totalrecs= count(id) from table1