Error Handling with CASE STATEMENT

  • Good afternoon!

    Can anyone assist me in converting this to a case statement?

    IF (@Variable NOT IN ('here', 'or here'))

    BEGIN

    SELECT @errorMsg = 'message here'

    GOTO ERROR

    END

    ¤ §unshine ¤

  • What exactly are you trying to convert to a case statement? You wouldn't put a goto or any control flow for that matter in a case statement.

  • Ok. Thank you.

    I was trying to change the IF to a CASE.

    CASE WHEN @Variable IN ().

    Thanks again!

    ¤ §unshine ¤

  • In MS SQL Server the CASE is not used for flow control. Your original post shows that you are attempting to control the flow of execution. What are you trying to accomplish by using CASE?

  • hi sunshine i hope this case statement work

    CASE

    WHEN (@Variable NOT IN ('here', 'or here'))

    THEN

    SELECT @errorMsg = 'message here'

    ELSE GOTO ERROR

    END

  • nagraj275 (6/3/2015)


    hi sunshine i hope this case statement work

    It won't, because as Lynn said above, CASE is not used for control flow in SQL Server.

    If you'd tried to parse the code you wrote, it would be abundantly clear that it won't work

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • sunshine-587009 (6/2/2015)


    Ok. Thank you.

    I was trying to change the IF to a CASE.

    CASE WHEN @Variable IN ().

    IF and CASE are not interchangeable. IF is used for control flow. Case is an expression used within a statement.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Ok. Thank you so much for your help! 🙂

    ¤ §unshine ¤

Viewing 8 posts - 1 through 7 (of 7 total)

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