I give an example to show my problem. I created a table like this:
CREATE TABLE a
(
id INT
)
I then created an AFTER INSERT
trigger to not allow insert id = 1
into table a
:
CREATE TRIGGER [dbo].[insert_a]
ON [dbo].[a] AFTER INSERT
AS
BEGIN
DECLARE @id INT
SELECT @id = id FROM inserted
IF @id = 1
BEGIN
RAISERROR('1', 12, 1)
ROLLBACK;
END
SELECT * FROM inserted
END
Then I insert id = 1
into table a
:
INSERT INTO a VALUES(1)
I get nothing from INSERTED
table.
I realize that when I ROLLBACK then + the data in table a
was rolled back (I know) and data in INSERTED
table is also removed. Why is that?
If I change the AFTER INSERT
trigger to an INSTEAD OF INSERT
trigger:
ALTER TRIGGER [dbo].[insert_a]
ON [dbo].[a] INSTEAD OF INSERT
AS
BEGIN
DECLARE @id INT
SELECT @id = id FROM inserted
IF @id = 1
BEGIN
RAISERROR('1', 12, 1)
ROLLBACK
END
SELECT * FROM inserted
END
INSERT INTO a VALUES(1)
Then I get the result:
id
1
That means data in INSERTED
table is not removed though have been ROLLBACK.
Help me explain deeply what happens inside trigger?
data in INSERTED table is also removed.Why is that?
are you sure ? I executed your code and everything seems fine!, also change RAISERROR command to beRAISERROR(' I am In the Begin and End block after inserting into inserted table',12,1)
just for more Clarification, also please notify the data is existing into inserted table only while executing DML commands then the data has gone. – ahmed abdelqaderinserted
can contain 0, 1 or multiple rows. If someone inserts multiple rows where some of them haveid
1 then a) What should the trigger do and b) Your current trigger only picks up one of theid
s so may not react. Why do you not just have acheck
constraint instead? – Damien_The_Unbeliever