在原生编译的存储过程中可以使用try..catch语句块。 支持以下构造:
错误行
错误信息
ERROR_NUMBER
错误程序
错误严重程度
错误状态
CREATE PROCEDURE test_try_catch
with native_compilation, schemabinding, execute as owner
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
BEGIN TRY
-- generate error
declare @i int = 1,
@j int = 0
select @i/@j
END TRY
BEGIN CATCH
-- Execute error retrieval routine.
SELECT
ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage
END CATCH
end
go
exec test_try_catch
go