在下面的示例中,SQL Server 2008 R2 Management Studio无法识别我的错误,它说
Throw附近的语法不正确
我正在尝试在此处引发错误,因此当有人两次插入相同的值时,我可以在我的网站中处理该错误。
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语句在 SQL Server 2012中* 引入 *
THROW
http://msdn.microsoft.com/en- us/library/ee677615.aspx
您可以RAISERROR改用。
RAISERROR
http://msdn.microsoft.com/zh- cn/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;