在 raiserror sqlserver 上终止进程

terminate the process upon raiserror sqlserver

我需要知道如何在遇到 raiserror 命令时停止进程

我尝试使用不同的状态代码,例如 1,-1,严重程度为 16。但它不会停止该过程。它显示错误并继续下一步。

我想要一种在遇到 raiserror 时停止剩余进程的方法

create procedure p_testing
as
begin
RAISERROR('Error is raised',16,1);
insert into blah(si,name) values(1,'teasting_raiserror');
END

Exec p_testing

使用THROWXACT_ABORT:

CREATE TABLE dbo.SomeTable (ID int IDENTITY(1,1), SomeString varchar(10));
GO

CREATE PROC dbo.SomeProc @SomeString varchar(10) AS
BEGIN
    
    SET XACT_ABORT ON;
    
    THROW 59000, N'A pointless error has been raised!', 16;

    INSERT INTO dbo.SomeTable (SomeString)
    VALUES (@SomeString);
END;
GO

EXEC dbo.SomeProc 'MyString';

--This statement won't be run
SELECT *
FROM dbo.SomeTable;
GO
--This will be, as in a separate batch
SELECT *, NULL AS AdditionalColumn
FROM dbo.SomeTable;
GO
--Clean up

DROP PROC dbo.SomeProc;
DROP TABLE dbo.SomeTable;