1
ALTER TRIGGER tr_EMPLOYEE2_FORINSERT
ON EMPLOYEE2
FOR INSERT
AS
BEGIN
--  SELECT * FROM INSERTED --INSERTED Table is a special table created for the purposes of Triggers, it is available only in the context of the trigger.
    DECLARE @ID INT
    SELECT @ID = ID FROM INSERTED

    INSERT INTO EMPAUDIT
    VALUES('New Employee with id = ' + cast(@id as nvarchar(5)) + ' is added at  ' + cast(getdate() as nvarchar(20)))
END
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448

1 Answers1

0

a. We have no idea what your EMPAUDIT table looks like. But presuming it has more than one column (most tables do), you ought to use a column list for your INSERT statement:

INSERT INTO EMPAUDIT (Column_To_Insert_Into)
VALUES('New Employee with id = ' + cast(@id as nvarchar(5)) +
       ' is added at  ' + cast(getdate() as nvarchar(20)))

b. However, the trigger it actually still broken. Why? Because inserted can contain multiple rows (or no rows). So in fact what we want is:

INSERT INTO EMPAUDIT (Column_To_Insert_Into)
SELECT 'New Employee with id = ' + cast(i.id as nvarchar(5)) +
       ' is added at  ' + cast(getdate() as nvarchar(20))
FROM inserted i

and then you don't need the rest of the code you've written in your trigger

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448