How to interrupt the insertion of multiple lines in a trigger

Since sql Server 2005. I have declared a trigger that starts "AFTER INSERT, UPDATE", in this trigger I use WHILE and CURSOR to loop in the rows of the INSERTED table. When I find a line that does not satisfy a specific condition:

I want the trigger to raise an error and not insert any of the lines that started the trigger (even those that have already satisfied my state). <--- I do not know how to do this!

Can you tell me how I can raise an error and prevent its installation?

+3
source share
2 answers

use kickbacks

IF <some condition>
BEGIN
   RAISERROR ('condition doesn't satisfy something', 16, 1)
   ROLLBACK TRANSACTION
END
+8
source

, , , , :

if exists (select PK from INSERTED where .....)
BEGIN
    --from @SQLMenace answer
    RAISERROR ('condition doesn't satisfy something', 16, 1)
    ROLLBACK TRANSACTION

END

: , ...

+3

Source: https://habr.com/ru/post/1707197/


All Articles