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).

    @Length int


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

 #procedure body

DECLARE @alphaVar varchar(10)

EXEC rnd_STR @alphaVar output

SELECT @alphaVar


  • Fixing mysql error #1064
  • T-SQL Stored Procedure help: Getting a person's friends from Friends table
  • Flattening 1-to-many relationship
  • Misnamed field in subquery leads to join
  • Error connecting to MSSQL using PHP
  • Counting the articles which don't have a price change in SQL Server
  • 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

  • Need help on regular expression in SQL Server
  • Deleting row on Foreign key relation
  • SQL: how to get all the distinct characters in a column, across all rows
  • Pass back original SQL operation row count, after trigger?
  • Incorrect syntax near non-existent colon
  • Separating multiple SQL Server statements running in batch
  • 4 Solutions collect form web for “How to call a stored procedure and return a value?”

    You say @alphaVar is varchar(10). In that case you need to use an output parameter as below. Return can only be used for integer types in stored procedures.

    @Length int,
    @alphaVar varchar(10) OUTPUT    
    SET @alphaVar = 'blah'
    /* Rest of procedure body*/
    DECLARE @alphaVar varchar(10) 
    EXEC rnd_STR 10, @alphaVar output
    SELECT @alphaVar

    Alternatively you could use a scalar UDF rather than a stored procedure.

    You’re calling syntax is wrong.

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

    See EXECUTE in the reference.

    Try this:

    EXEC @alphaVar = rnd_STR 10

    a work around of getting this code is to execute the stored procedure in your Management Studio it self and copy the SQL code

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.