Creates one or more new triggers, or re-creates an existing trigger. A trigger is a type of stored procedure that is often used to enforce integrity constraints, executing automatically when a user attempts a specified data modification statement on a specified table.
create [or replace] trigger [owner.]trigger_name on [owner.]table_name {for {insert , update} | instead of {insert, update, delete}} [order integer] [as [if update (column_name) [{and | or} update (column_name)]...] SQL_statements [if update (column_name) [{and | or} update (column_name)]... SQL_statements]...]
You cannot use a variable for a trigger name.
The name of the trigger is not changed when the trigger definition is replaced. The name of the new trigger definition must match the old name to be replaced. If the trigger name differs from any existing trigger, a new trigger is created and the old trigger is not dropped .
You cannot change the name of the table when a trigger is replaced. If an existing trigger is modified to associate the trigger with another table, then an error is raised indicating that the trigger already exists on another table and cannot be replaced.
instead of – creates and fills the inserted and deleted pseudo tables, which are used in the trigger to examine the rows that would have been modified by the original insert, delete, or update query.
You cannot change an "instead of" trigger to a "for" trigger, and vice versa.
You can change these actions when you use the or replace clause. For example, if the old trigger definition specifies all clauses, the replacement definition can specify all clauses, or a combination of the actions.
Trigger actions take effect when the user action (insert, update, or delete) is attempted. If multiple trigger actions are specified, they are grouped with begin and end.
You can change trigger conditions and actions when the trigger definition is replaced.
You can drop or add the if update and change the column name referenced by this clause.
create trigger reminder on titles for insert, update as print "Don't forget to print a report for accounting."
create trigger t1 on titleauthor for insert as if (select count (*) from titles, inserted where titles.title_id = inserted.title_id) = 0 begin print "Please put the book's title_id in the titles table first." rollback transaction end
create trigger t2 on publishers for update as if update (pub_id) and @@rowcount = 1 begin update titles set titles.pub_id = inserted.pub_id from titles, deleted, inserted where deleted.pub_id = titles.pub_id end
create trigger t3 on titleauthor for delete as begin delete titles from titles, deleted where deleted.title_id = titles.title_id delete titleauthor from titleauthor, deleted where deleted.title_id = titleauthor.title_id print "All references to this title have been deleted from titles and titleauthor." end
create trigger stopupdatetrig on titles for update as if update (title_id) and datename (dw, getdate ()) in ("Saturday", "Sunday") begin rollback transaction print "We don't allow changes to" print "primary keys on the weekend!" end if update (price) or update (advance) if (select count (*) from inserted where (inserted.price * inserted.total_sales) < inserted.advance) > 0 begin rollback transaction print "We don't allow changes to price or" print "advance for a title until its total" print "revenue exceeds its latest advance." end
create table EmployeeWest ( empid int primary key, empname varchar(30), empdob datetime, region char(5) constraint region_chk check (region='West')) create table EmployeeEast ( empid int primary key, empname varchar(30), empdob datetime, region char(5) constraint region_chk check (region='East’)) create view Employees as select * from EmployeeEast union all select * from EmployeeWest create trigger EmployeesInsertTrig on Employees instead of insert as begin insert into EmployeeEast select * from inserted where region = "East" insert into EmployeeWest select * from inserted where region = "West" end --will insert the data into the EmployeeEast table insert into Employees values (10, 'Jane Doe', '11/11/1967', 'East') --will insert the data into the EmployeeWest table insert into Employees values (11, 'John Smith', '01/12/1977', 'West') --will insert multiple rows into EmployeeEast and --EmployeeWest tables. Employee2 table includes employees --from both East and West. insert into Employees select * from Employee2
CREATE TABLE Employee_t (id int PRIMARY KEY, name varchar(20), salary binary (64)) --where the id and name columns are stored unencrypted, salary is --encrypted and id is a primary key. create view employee_v as select id, name, my_decrypt (salary) from employee_t CREATE TRIGGER EmployeeInsert ON employee_v INSTEAD OF INSERT AS BEGIN INSERT employee_t SELECT id, name, my_encrypt (salary) FROM inserted END CREATE TRIGGER employeeUpdate ON employee_v INSTEAD OF UPDATE AS BEGIN DELETE FROM employee_t WHERE id IN (SELECT id FROM deleted) INSERT employee_t SELECT id, name, my_encrypt (salary) FROM inserted END CREATE TRIGGER employeeDelete ON employee_v INSTEAD OF DELETE AS BEGIN DELETE FROM employee_t WHERE id IN (SELECT id FROM deleted) END
create trigger reminder on titles for insert, update as print "Don't forget to print a report for accounting." select object_id("reminder") ----------- 1312004674
The next command changes the message of the printed trigger when anyone tries to update data in the titles table using the or replace clause:
create or replace trigger reminder on titles for update as print "Don't forget to give a report to accounting." select object_id("reminder") ----------- 1312004674
To avoid seeing unexpected results due to changes in settings, run set rowcount 0 as your initial statement before executing create trigger. The scope of set is limited to only the create trigger command, and resets to your previous setting once the procedure exits.
A trigger fires only once per data modification statement. A complex query containing a while loop may repeat an update or insert many times, and the trigger is fired each time.
In versions earlier than SAP ASE 16.0, consecutive create trigger commands dropped the old trigger and replaced it with a new trigger definition. However, the auditing options for the trigger were also dropped. Using the optional or replace clause, the definition is replaced, and auditing options are preserved.
In SAP ASE version 16.0 and later, when there are multiple triggers, specifying create without or replace raises an error if the trigger name is same. If you specify a different trigger name, a new trigger is created and the old trigger remains.
In versions of SAP ASE earlier than 16.0, if an existing trigger was replaced with the new trigger definition by specifying create without or replace, the name of the new trigger did not need to be same as the name of the old trigger name.
You can only use the order integer clause with for {insert | update | delete}; you cannot use it with instead of {insert | update | delete} triggers.
If you use a duplicate number for order, SAP ASE reports an error. order numbers need not be consecutive; in fact, nonconsecutive numbers might be preferable, as they allow you to insert new triggers into the middle of an order.
See also sp_commonkey, sp_configure, sp_depends, sp_foreignkey, sp_help, sp_helptext, sp_primarykey, sp_rename, sp_spaceused in Reference Manual: Procedures.
ANSI SQL – Compliance level: Transact-SQL extension.
Permissions on objects at trigger creation – when you create a trigger, the SAP ASE server makes no permission checks on objects such as tables or views that the trigger references. Therefore, you can successfully create a trigger, even though you do not have access to its objects. All permission checks occur when the trigger fires.
If the trigger and its objects are not owned by the same user, the user who caused the trigger to fire must have been granted direct access to the objects. For example, if the trigger performs a select from a table the user cannot access, the trigger execution fails. In addition, the data modification that caused the trigger to fire is rolled back.
If a trigger and its objects are owned by the same user, special rules apply. The user automatically has implicit permission to access the trigger’s objects, even though the user cannot access them directly. See the detailed description of the rules for implicit permissions in the System Administration Guide.
Permissions for instead ofand for triggers instead of – instead of triggers have the same permission requirements as for triggers: to create a view with instead of triggers, permission for insert/update/delete for the view, not the underlying tables, must be granted to the user.
Any user who impersonates the trigger owner through an alias or setuser cannot replace the trigger.
The following describes permission checks for create trigger that differ based on your granular permissions settings.
Setting | Description |
---|---|
Enabled | With granular permissions enabled, you must be the table owner and the create trigger privilege must not have been revoked to create a trigger. To create a trigger for another user's table, you must have the create any trigger privilege. You must be the trigger owner to replace the trigger. |
Disabled | With granular permissions disabled, only a system security officer can grant or revoke permissions to create triggers. A user with sa_role has implicit permission to create a trigger on any user table. Users can create triggers only on tables that they own. You must be the trigger owner to replace the trigger. The system security officer may revoke user permission to create triggers. Revoking permission to create triggers affects only the database in which the systems security officer issues the revoke command. Permission to run the create trigger command is restored to the users whose permission was revoked when the system security officer explicitly grants them create trigger privilege. |
Values in event and extrainfo columns of sysaudits are:
Information | Values |
---|---|
Event | 12 |
Audit option | create |
Command or access audited | create trigger |
Information in extrainfo |
|