Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server:插入后预提交触发器_Sql Server_Database_Triggers - Fatal编程技术网

Sql server SQL Server:插入后预提交触发器

Sql server SQL Server:插入后预提交触发器,sql-server,database,triggers,Sql Server,Database,Triggers,我已经查找了一段时间了,我找不到任何方法在插入发生后但在提交发生之前触发触发器。我需要这样做,以便我能够在插入的信息出现问题时返回角色,但也需要检查插入的信息,以便在插入完成后返回角色 是否有一个触发器可以执行此操作,或者有任何方法可以重现相同的功能?请参阅触发器后的说明 AFTER指定仅当所有操作 已成功执行触发SQL语句中指定的 您可以轻松地编写AFTER触发器,但不能使用它来控制在提交事务之前发生的事情。可能有一个显式事务保持打开状态,直到“手动”回滚或提交为止。触发器在提交发生之前被激活

我已经查找了一段时间了,我找不到任何方法在插入发生后但在提交发生之前触发触发器。我需要这样做,以便我能够在插入的信息出现问题时返回角色,但也需要检查插入的信息,以便在插入完成后返回角色

是否有一个触发器可以执行此操作,或者有任何方法可以重现相同的功能?

请参阅触发器后的说明

AFTER指定仅当所有操作 已成功执行触发SQL语句中指定的


您可以轻松地编写AFTER触发器,但不能使用它来控制在提交事务之前发生的事情。可能有一个显式事务保持打开状态,直到“手动”回滚或提交为止。

触发器在提交发生之前被激活。在触发器中,如果某些检查失败,您可以使用
回滚

CREATE TABLE dbo.Product(
    Id INT NOT NULL PRIMARY KEY,
    Name NVARCHAR(100) NOT NULL,
    UnitPrice NUMERIC(9,2) NOT NULL -- CHECK (UnitPrice > 0)
);
GO
CREATE TRIGGER trgIU_Product_VerifyUnitPrice
ON dbo.Product
AFTER INSERT, UPDATE
AS
BEGIN
    IF UPDATE(UnitPrice)
    BEGIN
        -- For simple checks you could use CHECK constraints
        -- inserted and deleted are virtual tables store the new and the old rows (values)
        IF EXISTS(SELECT * FROM inserted WHERE UnitPrice <= 0)
        BEGIN
            ROLLBACK; -- It "cancels" current transaction
            RAISERROR('Wrong UnitPrice.',16,1); -- It notifies the caller that there is an error
        END
    END
END;
GO

SET NOCOUNT ON;
PRINT 'Test #1';
INSERT INTO dbo.Product (Id,Name,UnitPrice)
SELECT  1 , 'PCs      ', 1200;

PRINT 'Test #2';
INSERT INTO dbo.Product (Id,Name,UnitPrice)
SELECT  2 , 'MACs     ', 2200;

PRINT 'Test #3';
INSERT INTO dbo.Product (Id,Name,UnitPrice)
SELECT  3 , 'Keyboard ', 0;

PRINT 'Test #4';
INSERT INTO dbo.Product (Id,Name,UnitPrice)
SELECT  4 , 'AAA', 111
UNION ALL
SELECT  5 , 'BBB', 0;
GO

PRINT 'Test #5';
BEGIN TRANSACTION;
INSERT INTO dbo.Product (Id,Name,UnitPrice)
SELECT  6 , 'CCC', 222
UNION ALL
SELECT  7 , 'DDD', 0;
COMMIT TRANSACTION;
GO
SELECT @@TRANCOUNT AS [Active transactions count];
GO

PRINT 'Test #6';
SELECT * FROM dbo.Product;
参考资料:

/*
Test #1

Test #2

Test #3
Msg 50000, Level 16, State 1, Procedure trgIU_Product_VerifyUnitPrice, Line 11
Wrong UnitPrice.
Msg 3609, Level 16, State 1, Line 11
The transaction ended in the trigger. The batch has been aborted.

Test #4
Msg 50000, Level 16, State 1, Procedure trgIU_Product_VerifyUnitPrice, Line 11
Wrong UnitPrice.
Msg 3609, Level 16, State 1, Line 2
The transaction ended in the trigger. The batch has been aborted.

Test #5
Msg 50000, Level 16, State 1, Procedure trgIU_Product_VerifyUnitPrice, Line 11
Wrong UnitPrice.
Msg 3609, Level 16, State 1, Line 3
The transaction ended in the trigger. The batch has been aborted.
Active transactions count
-------------------------
0

Test #6
Id Name UnitPrice
-- ---- ---------
1  PCs  1200.00
2  MACs 2200.00
*/