Raise an error manually in T-SQL to jump to BEGIN CATCH block

前端 未结 5 945
忘掉有多难
忘掉有多难 2021-02-03 19:12

Is it possible to raise an error in a stored procedure manually to stop execution and jump to BEGIN CATCH block? Some analog of throw new Exception() i

5条回答
  •  一向
    一向 (楼主)
    2021-02-03 19:32

    You're looking for RAISERROR.

    From MSDN:

    Generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY…CATCH construct.

    CodeProject has a good article that also describes in-depth the details of how it works and how to use it.

提交回复
热议问题