且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

T-SQL中的TRY和RAISERROR

更新时间:2023-02-06 14:15:47

这是因为RAISERROR的严重性不够高,需要在11到19之间, a href =http://msdn.microsoft.com/en-us/library/ms178592(SQL.90).aspx =noreferrer> here



例如

  RAISERROR('发现错误,请修复这些错误并重试',16,2)WITH SETERROR 


Having a small issue and wondering if I'm using these correctly.

In my SQL script is have

BEGIN TRY
    // check some information and if there are certains errors
    RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR

    // Complete normal process if no errors encountered above
    PRINT 'IMPORT SUCCEEDED'
END TRY
BEGIN CATCH
    PRINT 'IMPORT ABORTED. ERRORS ENCOUNTERED'
END CATCH

However, this is encountering an error and then continuing with the rest of the script. What am I missing? Thanks!

It's because the severity of the RAISERROR is not high enough, needs to be between 11 and 19, as described here

e.g.

RAISERROR ('Errors found, please fix these errors and retry', 16, 2) WITH SETERROR