Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Procedure Select into a variable

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.

like image 725
iosdevnyc Avatar asked Mar 01 '10 00:03

iosdevnyc


2 Answers

like this

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

--will count NULLS
select @totalrecs= count(*) from table1
like image 165
SQLMenace Avatar answered Oct 02 '22 11:10

SQLMenace



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

Exec sp_DoSomething @Var = @totalCount
like image 35
Leigh S Avatar answered Oct 02 '22 10:10

Leigh S