简体   繁体   English

我无法使用THROW SQL Server 2008 R2

[英]I am unable to use THROW SQL Server 2008 R2

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

THROW Statement is introduced in SQL Server 2012 THROW语句在SQL Server 2012中引入

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

You can use RAISERROR instead. 您可以改用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;

Use RAISERROR instead 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM