Error in Raiserror After upgrading SqlServer 2008 R2 to 2017



  • Please advise and share your solution.

    In 2008 R2

    DECLARE @ErrMsg varchar (25) 
    SET @ErrMsg = 'Test Raiserror in 2008 R2'
    RAISERROR 50002 @ErrMsg
    

    The above statement produces the following output.

    Msg 50002, Level 16, State 1, Line 4 Test Raiserror in 2008 R2 Completion time: 2022-03-12T20:12:20.1296499+03:00

    In 2017

    DECLARE @ErrMsg varchar (25) 
    SET @ErrMsg = 'Test Raiserror in 2017'
    RAISERROR 50002 @ErrMsg
    

    The above statement produces the following output.

    Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '50002'. Completion time: 2022-03-12T20:13:24.4256066+03:00



  • RAISERROR 50002 @ErrMsg
    

    This form of RAISERROR syntax was deprecated many versions ago and removed from the product entirely in the SQL Server 2012 release. From the https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ms144262(v=sql.110) :

    Discontinued feature: RAISERROR in the format RAISERROR integer 'string' is discontinued.

    Replacement: Rewrite the statement using the current RAISERROR(…) syntax.

    As an alternative to the https://docs.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?view=sql-server-ver15 , you could instead use https://docs.microsoft.com/en-us/sql/t-sql/language-elements/throw-transact-sql?view=sql-server-ver15 as below. This allows use of the same same user-defined error number without having to add message to sys.messages (which RAISERROR requires). Be sure to terminate the preceding statement with a semi-colon.

    THROW 50002, @ErrMsg, 1;
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2