Return Value not coming back from SQL Stored Procedure

I'm trying to retrieve the max sort order from a table using the stored procedure showing in the code window but am having a problem getting the return value

In SQL Management Studio (2005) I executed the procedure using the following code:
 
DECLARE      @return_value int
 
EXEC      @return_value = [dbo].[ModDev_Get_MaxSortOrder]
            @PlaylistID = 1
 
SELECT      'Return Value' = @return_value
 
 
These are the results that I get:
 
MaxSortOrder
------------
8
 
(1 row(s) affected)
 
Return Value
------------
0
 
(1 row(s) affected)
 
 
Shouldn't the Return Value also be 8?  In my code I'm getting 0 returned from the stored proc and I'm not sure what I'm doing wrong.  Please advise.

CREATE PROCEDURE [dbo].[ModDev_Get_MaxSortOrder]
(
     @PlaylistID int
)
AS
BEGIN
     SELECT
         ISNULL(MAX([SortOrder]), 0) AS MaxSortOrder
     FROM
         [ModDev_PlaylistEntries]
     WHERE
         [PlaylistID] = @PlaylistID
     AND
        [IsActive] = 'Y'
END
 
 
 

Open in new window

DanNetwork EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>Shouldn't the Return Value also be 8?
no, because you don't assign the value to the return value
CREATE PROCEDURE [dbo].[ModDev_Get_MaxSortOrder]
(
     @PlaylistID int
)
AS
BEGIN 
 DECLARE @res INT 
     SELECT @res =
         ISNULL(MAX([SortOrder]), 0) AS MaxSortOrder
     FROM
         [ModDev_PlaylistEntries]
     WHERE
         [PlaylistID] = @PlaylistID
     AND
        [IsActive] = 'Y' 
  RETURN @res
END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
alternative to use a OUTPUT parameter:
CREATE PROCEDURE [dbo].[ModDev_Get_MaxSortOrder]
(
     @PlaylistID int
   , @res int OUTPUT
)
AS
BEGIN
     SELECT @res = 
         ISNULL(MAX([SortOrder]), 0) AS MaxSortOrder
     FROM
         [ModDev_PlaylistEntries]
     WHERE
         [PlaylistID] = @PlaylistID
     AND
        [IsActive] = 'Y'
END 
 

and usage: 
DECLARE      @return_value int
 
EXEC      [dbo].[ModDev_Get_MaxSortOrder]  @PlaylistID = 1, @return_value OUTPUT
 
SELECT      @return_value [Return Value] 

Open in new window

0
DanNetwork EngineerAuthor Commented:
Thanks angelll ... that was a big help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.