简体   繁体   中英

MS sql server-Stored procedure

Anyone can help me to find the error in this code, when I execute it says

"Procedure or function 'dep_cnt' expects parameter '@cnt', which was not supplied."

I want to count number of departments in colombo.

CREATE PROCEDURE dep_cnt
 @dep_loc varchar(10),
 @cnt int
AS
SELECT @Cnt = count(*) FROM Department WHERE Dept_Loc = @dep_loc

And

EXEC dep_cnt @dep_loc = 'colombo'

You can either pass a @cnt parameter with a value to your stored procedure call, or set a default of NULL to the @cnt parameter inside the procedure definition, like:

CREATE PROCEDURE dep_cnt 
    @dep_loc varchar(10)
  , @cnt int = NULL 
AS 
    SELECT @Cnt = count(*) 
    FROM Department 
    WHERE Dept_Loc = @dep_loc

EXEC dep_cnt @dep_loc = 'colombo'

Although I don't see much value in this, maybe you're looking for @cnt as an output / returned value?

CREATE PROCEDURE dep_cnt 
    @dep_loc varchar(10)
  , @cnt int OUTPUT
AS 
    SELECT @Cnt = count(*) 
    FROM Department 
    WHERE Dept_Loc = @dep_loc

DECLARE @cntResult int    

EXEC dep_cnt @dep_loc = 'colombo', @cnt = @cntResult OUTPUT

You can find more info here about returning data from a stored procedure .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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