Return Code for an If Statement

  • Hello,  I am writing a Stored Procedure and would like to return an error code for a web application if a specific value passed is NULL.  I have attempted to do this with an IF STATEMENT but have since learned this is not possible.  What would be the correct way to accomplish this?  Below is my sample code.

    DECLARE @myvalue varchar(4)

       If @myvalue IS NULL
        BEGIN
        PRINT 'Stop Processing'
        RETURN -3
        END

  • Try the following. I set @myvalue to 6 as a test for a normal operation. Comment it out and you'll get a "-3" returned.
    Build your other logic around/in it. 


    DECLARE @myvalue int
    SET @myvalue = 6 

    begin
    select ISNULL(@myvalue, -3)
    end

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • rjjh78 - Thursday, November 2, 2017 1:08 PM

    Hello,  I am writing a Stored Procedure and would like to return an error code for a web application if a specific value passed is NULL.  I have attempted to do this with an IF STATEMENT but have since learned this is not possible.  What would be the correct way to accomplish this?  Below is my sample code.

    DECLARE @myvalue varchar(4)

       If @myvalue IS NULL
        BEGIN
        PRINT 'Stop Processing'
        RETURN -3
        END

    Why do you say it's not possible? Here's an example from Microsoft themselves (link)

    USE AdventureWorks2012;
    GO

    CREATE PROCEDURE checkstate @param VARCHAR(11)
    AS
    IF
    (
      SELECT StateProvince
      FROM Person.vAdditionalContactInfo
      WHERE ContactID = @param
    ) = 'WA'
      RETURN 1;
    ELSE
      RETURN 2;
    GO

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Henrico Bekker - Thursday, November 2, 2017 1:19 PM

    Try the following. I set @myvalue to 6 as a test for a normal operation. Comment it out and you'll get a "-3" returned.
    Build your other logic around/in it. 


    DECLARE @myvalue int
    SET @myvalue = 6 

    begin
    select ISNULL(@myvalue, -3)
    end

    That doesn't provide the conditional early exit though.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • rjjh78 - Thursday, November 2, 2017 1:08 PM

    Hello,  I am writing a Stored Procedure and would like to return an error code for a web application if a specific value passed is NULL.  I have attempted to do this with an IF STATEMENT but have since learned this is not possible.  What would be the correct way to accomplish this?  Below is my sample code.

    DECLARE @myvalue varchar(4)

       If @myvalue IS NULL
        BEGIN
        PRINT 'Stop Processing'
        RETURN -3
        END

    It works just fine on my machine.  Understand that you can't include the RETURN value when running a script from SSMS.  It will, however, work in a stored procedure with the -3.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply