Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a stored procedure and return a value?

Hey all, I have a stored procedure and I need to call it within another stored procedure, but I want the first one to return a value (field value).

CREATE PROCEDURE rnd_STR
(
    @Length int

)

@alphaVar varchar(10) OUTPUT
AS
SET @alphaVar = 'blah'

 #procedure body
END
GO

DECLARE @alphaVar varchar(10)

EXEC rnd_STR @alphaVar output

SELECT @alphaVar

ERRORS

Msg 102, Level 15, State 1, Procedure rnd_STR, Line 6

Incorrect syntax near '@alphaVar'.

Msg 137, Level 15, State 1, Procedure rnd_STR, Line 8

Must declare the scalar variable "@alphaVar".

Msg 2812, Level 16, State 62, Line 4

Could not find stored procedure 'rnd_STR'.

(1 row(s) affected)

didn't work !!

How can I call it??

BTW, the returned @ID is a string

like image 833
Lisa Avatar asked Dec 29 '10 23:12

Lisa


1 Answers

You're calling syntax is wrong.

 DECLARE @newId int
 EXEC @newId = rnd_STR, @length = 10

See EXECUTE in the reference.

like image 103
RB. Avatar answered Oct 24 '22 00:10

RB.