Introduction
Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be executed in response to any of the following events −
1- A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).2- A database definition (DDL) statement (CREATE, ALTER, or DROP).3- A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Triggers could be defined on the table, view, schema, or database with which the event is associated.
Creating Trigger-
Before creating Trigger i am taking one example suppose we have one table "DIMCUSTOMER" and we want that when anyone is inserting any value into this table at that time user name date and system date is stored means who have inserted and when.
Create a table which will store the user name and time who have inserted data in "DIMCUSTOMER" table.
Right click-->New Tabletable name "STSAUDIT" with 2 fields
Now open sql Editor and paste the below code
Code Explanation
create trigger STS.STSTRIGGER --> will create a trigger
after insert on "STS".DIMCUSTOMER" for each row --> means each insert of row in the table "STS".DIMCUSTOMER"
begin --> start of trigger
INSERT INTO "STS"."STSAUDIT" VALUES(
CURRENT_TIMESTSMP, CURRENT_USER
) ;above line means insert current time and user name into the table "STS"."STSAUDIT"
end --> end of trigger
After executing above code you can see one Trigger is created into trigger folder
Now open the table "STS"."STSAUDIT" and check timestamp and user name is inserted or not
Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be executed in response to any of the following events −
1- A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).2- A database definition (DDL) statement (CREATE, ALTER, or DROP).3- A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Triggers could be defined on the table, view, schema, or database with which the event is associated.
Creating Trigger-
Before creating Trigger i am taking one example suppose we have one table "DIMCUSTOMER" and we want that when anyone is inserting any value into this table at that time user name date and system date is stored means who have inserted and when.
Create a table which will store the user name and time who have inserted data in "DIMCUSTOMER" table.
Right click-->New Tabletable name "STSAUDIT" with 2 fields
Now open sql Editor and paste the below code
Code Explanation
create trigger STS.STSTRIGGER --> will create a trigger
after insert on "STS".DIMCUSTOMER" for each row --> means each insert of row in the table "STS".DIMCUSTOMER"
begin --> start of trigger
INSERT INTO "STS"."STSAUDIT" VALUES(
CURRENT_TIMESTSMP, CURRENT_USER
) ;above line means insert current time and user name into the table "STS"."STSAUDIT"
end --> end of trigger
After executing above code you can see one Trigger is created into trigger folder
Now open the table "STS"."STSAUDIT" and check timestamp and user name is inserted or not
No comments:
Post a Comment