If you have sql management studio installed , create 2 tables for our example.
Below you have database = one
- event_tags : columns =
- event_id ( int)
- tag_id (int)
- events : columns =
- id (int)
- info (nvarchar)
Expand the tables , find triggers , right click and add new trigger
Add trigger code as below :
create TRIGGER AfterInsertTrigger
AS declare @id int,@tag int;
select @id=i.id from inserted i;
select @tag=i.info from inserted i;
insert into event_tags(event_id,tag_id)
What does the above code perform ?
- When values are inserted in table events
- insert command is triggered and values are inserted into table event_tags
Hence there is no need keep on checking if values are being inserted into table events , triggers does them for you and automate the next action
Now insert values into table event and check event_tags ! ..There you go ,you have triggered successfully !