我无法使用 THROW SQL Server 2008 R2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16670985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:20:39  来源:igfitidea点击:

I am unable to use THROW SQL Server 2008 R2

sqlsql-serversql-server-2008-r2try-catchthrow

提问by user2405574

SQL Server 2008 R2 Management Studio does not recognized my throw in the below example, it says

SQL Server 2008 R2 Management Studio 无法识别我在以下示例中的投掷,它说

incorrect syntax near Throw

Throw 附近的语法不正确

I am trying to throw an error here, so I can handled it in my website when someone insert the same value twice.

我试图在这里抛出一个错误,所以当有人两次插入相同的值时,我可以在我的网站中处理它。

Begin Try
 insert into BusinessID (BusinessID) values (@ID)
 insert into BusinessID (BusinessID) values (@ID)

End Try

Begin Catch

Print 'PK already exist'
THROW
End Catch

回答by Nenad Zivkovic

THROWStatement is introduced in SQL Server 2012

THROWSQL Server 2012 中引入了语句

http://msdn.microsoft.com/en-us/library/ee677615.aspx

http://msdn.microsoft.com/en-us/library/ee677615.aspx

You can use RAISERRORinstead.

你可以RAISERROR改用。

http://msdn.microsoft.com/en-us/library/483588bd-021b-4eae-b4ee-216268003e79(v=sql.105)

http://msdn.microsoft.com/en-us/library/483588bd-021b-4eae-b4ee-216268003e79(v=sql.105)

BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;

回答by Pawan

Use RAISERRORinstead of Throw in your sql block.

在 sql 块中使用RAISERROR而不是 Throw。

Begin Try
 insert into BusinessID (BusinessID) values (@ID)
 insert into BusinessID (BusinessID) values (@ID)

End Try

Begin Catch

Print 'PK already exist'
  DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

SELECT 
    @ErrorMessage = ERROR_MESSAGE(),
    @ErrorSeverity = ERROR_SEVERITY(),
    @ErrorState = ERROR_STATE();

-- Use RAISERROR inside the CATCH block to return error
-- information about the original error that caused
-- execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, -- Message text.
           @ErrorSeverity, -- Severity.
           @ErrorState -- State.
           );
End Catch