Write log of a field only if it is insert (trigger SQL Server)

1

I have a trigger that fires when insert / update FOR UPDATE, INSERT I check if the field has changed with:

if update (nome)
 --faça algo

How do I know if I am inserting a record in the table to be able to record a log?

    
asked by anonymous 20.07.2016 / 15:42

1 answer

2

Jeterson, I have more knowledge in Oracle, but researching a bit with key terms, I think it will solve your problem.

  

SQL_STATEMENT

     

These are the conditions and actions of the trigger. Trigger conditions specify additional criteria that determine whether   DML, DDL, or logon events cause the trigger actions to be   executed.

     

A trigger is created to check or change data based on a   definition or modification of data. He should not return   to the user.

     

DML triggers use the logical (conceptual) tables deleted and    inserted .
As Documentation - CREATE TRIGGER   (Transact-SQL)

  

The deleted table stores copies of affected rows during   instructions DELETE and UPDATE . While executing a DELETE   or% with%, the rows are excluded from the table of triggers and   transferred to the UPDATE table. The deleted table and the   triggers generally do not have lines in common.
As per Documentation - Use inserted and excluded tables   (Transact-SQL)

According to the documentation, to know if you are inserting a record, you can do the following:

IF EXISTS (SELECT 1 FROM deleted)
  -- update
    
20.07.2016 / 16:47