I'm trying to create a trigger which is operating on table component and logging UPDATE, DELETE and INSERT operation to the table component_history
CREATE TRIGGER component_hist
ON component
AFTER INSERT, UPDATE, DELETE
AS
DECLARE
@tr_action varchar(8),
@tr_id_compoment int,
@tr_name varchar(max),
@tr_value int
IF COLUMNS_UPDATED() <> 0 -- delete or update?
BEGIN
SET @tr_action = 'UPDATE'
ELSE -- delete
BEGIN
SET @tr_action = 'DELETE'
END
INSERT INTO component_history VALUES (@tr_id_component, @tr_name, @tr_value, @tr_action);
How can I send information from columns (id, name, value) from table component into component_history?
I've tried:
SET
@tr_id_component = component.id,
@tr_name = component.name,
@tr_value = component.value
but it reports:
Msg 4104, Level 16, State 1, Procedure component_hist, Line 22
The multi-part identifier "component.id" could not be bound.
EXISTS (SELECT * FROM component)will always be true. And triggers fire once per statement, not once per row, so thinking of putting values into variables is always wrong-headed.