[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Return Value not coming back from SQL Stored Procedure

Posted on 2008-02-12
3
Medium Priority
?
1,285 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:afacts
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20877671
>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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20877690
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
 

Author Closing Comment

by:afacts
ID: 31430283
Thanks angelll ... that was a big help!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question