Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an output parameter to the value of count() in sql stored procedure

I have an output parameter @Counter and a temporary table #tmpUsers

Is it possible to assign the value of

SELECT COUNT(*) FROM #tmpUsers

To the @Counter output parameter?

I have tried

SET @Counter = SELECT COUNT(*) FROM #tmpUsers

But this doesn't work for me

like image 722
Win Avatar asked Aug 06 '13 10:08

Win


People also ask

How do I pass an output parameter to a SQL stored procedure?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

What are output parameters in stored procedure?

Output parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module.

How do you assign a value to a variable in SQL stored procedure?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.


1 Answers

Try this way:

SELECT @Counter = COUNT(*) 
FROM #tmpUsers

or

SET @Counter = (SELECT COUNT(*) FROM #tmpUsers)
like image 68
Robert Avatar answered Nov 14 '22 20:11

Robert