Save a field log only if it is Insert (Trigger SQL Server)

Asked

Viewed 393 times

1

I have a Rigger that fires when it is Insert/update FOR UPDATE, INSERT I check if the field was changed with:

if update (nome)
 --faça algo

How do I know if I am entering a record in the table to record a log?

1 answer

2


Jeterson, I have more knowledge in Oracle, but searching a little with key terms, I believe it will solve your problem.

SQL_STATEMENT

It is the conditions and actions of the trigger. The trigger conditions specify additional criteria that determine whether the DML, DDL or logon events cause trigger actions to be executed.

A trigger is created to verify or change data based on a definition statement or data modification. It should not return user data.

DML triggers use logic tables (conceptual) deleted and inserted.
In accordance with Documentation - CREATE TRIGGER (Transact-SQL)

The table deleted stores copies of affected lines during instructions DELETE and UPDATE. During the execution of an instruction DELETE or UPDATE, lines are excluded from the triggers table and transferred to the table deleted. The table deleted and the table of triggers usually have no lines in common.
In accordance with Documentation - Use inserted and deleted tables (Transact-SQL)

As documentation, to know if you are entering a record, you can do as follows:

IF EXISTS (SELECT 1 FROM deleted)
  -- update

  • 1

    Oops, it came out right, I checked it this way: IF NOT EXISTS (SELECT * FROM DELETED) . Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.