Prevent insert on trigger and perform an update instead - sql-server

I have a trigger that is raised after an insert or update. In case of an insert, I want to check if the record being inserted on table exists. If exists, then I want to ignore the insert and perform an update within trigger for that record. How can I do this? Is is possible to do it within an after insert,update trigger (not using instead of trigger)?

Related

Can we use both instead of and after in triggers?

I want to define instead of for insert and update operations but after for the delete operation.
Is it possible? Or, do I need to write an additional trigger for that? For example, no rows can be removed.
CREATE TRIGGER test_trigger ON my_table
instead of INSERT,UPDATE for DELETE
AS
BEGIN
.......
END
It seems you would need 2 triggers. Only 1 INSTEAD OF trigger may be create per DML action type per table, although DML action types may be combined, as in this case, i.e. both INSERT and UPDATE.
CREATE TRIGGER test_trigger_instead_of_ins_upd ON my_table
instead of INSERT,UPDATE
AS
BEGIN
.......
END
---------------------------------------------------------
CREATE TRIGGER test_trigger_after_del ON my_table
after DELETE
AS
BEGIN
.......
END

Trigger to update a record to null whenever there is an insert or update on a row in the table in Oracle DB

Need help in writting a Trigger to update a record to NULL whenever there is an insert or update on a row in the table in Oracle DB.
CODE
create or replace trigger updateRowToNull
AFTER
UPDATE OR INSERT
ON CUSTOMER for each row
BEGIN
update customer set customeraddr=NULL;
END;
A trigger cannot modify the table to which it is attached with a separate transaction. A "before" trigger can, however, manipulate column values within the transaction that causes it to be fired.
create or replace trigger update_row_to_null
before update or insert on customer
for each row
begin
:new.customeraddr := null;
end;

SQL Server Trigger after insert update and delete

Want to create a trigger where I have two table "A" and "B" and table "A" has already some records in it so i want to write a trigger in table "B" where i want to check condition if table "A" has already the record exist which is inserting then do not insert or fire a trigger of table "B" if record is not exist in table "A" then fire a trigger and insert a record into the table.
As SQL Server doesn't have BEFORE INSERT triggers to alter that insertion before its done, I would use an INSTEAD OF INSERT trigger (your trigger code is executed instead of the usual insertion, so you can add your checks and/or additional actions).
This trigger sample will only insert rows in A when they don't exist in B:
CREATE TRIGGER tr_a_ioi ON A
INSTEAD OF INSERT
AS BEGIN
insert into A
select I.*
from inserted I
left join B on B.PK_Field = I.PK_Field
where B.PK_Field is null -- no corresponding row is found in B
END
You can write similar INSTEAD OF UPDATE and INSTEAD OF DELETE triggers to do additional checks or actions when trying to update or delete rows in A.

Is an INSERT trigger recursive, or cause an infinite loop?

I have an INSERT trigger on table. When a row is inserted in table, the INSERT trigger is fired.
The trigger's behaviour is such that it will insert another row into the same table.
What would be the result of the INSERT statement?
Does this INSERT result in an infinite loop, or just the expected 2 inserts?
This is a setting in SQL- see the CREATE TRIGGER msdn page, specifically the section on Recursive Triggers. The setting you need to look into is RECURSIVE_TRIGGERS, if this is false, a trigger on Table1 will not trigger another insert into Table1. If you do allow recursive triggers, the limit is 32 levels deep.

SQL Server trigger execution

Say I have an UPDATE trigger on tableA that inserts a new record into tableB.
CREATE TRIGGER insertIntoTableB
ON tableA
FOR UPDATE
AS
INSERT INTO tableB (...) VALUES (...)
GO
I then run these statements sequentially. Will the second UPDATE statement (UPDATE tableB) work OK? (i.e. wait for the trigger on table A to fully execute)
UPDATE tableA
SET ...
WHERE key = 'some key'
UPDATE tableB
SET ...
WHERE key = 'newly inserted key from trigger'
The behavior is subject to the nested triggers server configuration, see Using Nested Triggers:
Both DML and DDL triggers are nested
when a trigger performs an action that
initiates another trigger. These
actions can initiate other triggers,
and so on. DML and DDL triggers can be
nested up to 32 levels. You can
control whether AFTER triggers can be
nested through the nested triggers
server configuration option. INSTEAD
OF triggers (only DML triggers can be
INSTEAD OF triggers) can be nested
regardless of this setting.
When a trigger on table A fires and inside the trigger table B is updated, the trigger on table B runs immediately. The Table A trigger did not finish, it is blocked in waiting for the UPDATE statement to finish, which in turn waits for the Table B trigger to finish. However, the updates to table A have already occurred (assuming a normal AFTER trigger) and querying the Table A from the table B's trigger will see the updates.
If the updates are sequentially coded into the UPDATE trigger of A then yes.

Resources