TSQL For Triggers - tsql

Sorry but I'm struggling with the online documentation.
What exactly is the difference for a FOR trigger compared to a AFTER and INSTEAD OF?
Thanks

The FOR and the AFTER trigger is the same. You specify them in the same way and they do the same thing:
CREATE TRIGGER sometrigger on sometable FOR INSERT,UPDATE,DELETE
CREATE TRIGGER sometrigger on sometable AFTER INSERT,UPDATE,DELETE
The INSTEAD OF trigger is something completely different. When you specify a trigger with the instead of then the trigger will execute instead of the insert, update and delete. This can be useful when you have views. So you can control what should be delete, updated and inserted in the views underlying tables
Here is an link to explain the triggers in more depth

Related

PostgreSql Event trigger for Auditing

I am about to implement an audit system in my database and am looking for the most effective way to do this.
I've read the wiki and did my research.
There is this example: https://wiki.postgresql.org/wiki/Audit_trigger
And also this: https://wiki.postgresql.org/wiki/Audit_trigger_91plus
In the listed examples you have to create a trigger for each table (kinda redudant).
However with Postgres 9.3+ we have the ability of event triggers where we can react on special events like create, alter or drop table.
An optimal solution would be:
create event trigger UpdateTables
on ddl_command_end
when tag in ( 'insert ', 'update ')
execute procedure DoAudit();
However that returns:
ERROR: filter value "insert " not recognized for filter variable "tag"
SQL Status:42601
I am wondering if we also can react on insert, update events with event triggers and use this single trigger for all auditing?
This won't work.
The "on ddl_command_end" means the event is intended to be invoked on structural changes in the database. The complete reference to events is here: https://www.postgresql.org/docs/current/static/event-trigger-matrix.html
Have a look at this:
https://eager.io/blog/audit-postgres/
This is a quite simple, yet effective way to implement an auditing system, making use of the hstore data type. I recently tried it and it worked flawlessly "out of the box".

is it possible to write a generic trigger with parametrized table name in it?

i have to add trigger to multiple tables but they all basically write to an audit table when insert, update, delete are triggered.
the only change really is the table name on the "create trigger" line.
i am wondering if it is possible to write something like "
create trigger {##table_}trigger on ##table for insert, update, delete
I am trying to replace {##table_}trigger with the ${tableName_}trigger. i.e. if a table is name userTable, it would be userTable_trigger.
I would appreciate if someone can point out if this is feasible at all. as far as I can tell from the doc, it doesn't seem to be the case

execute a trigger when I create a table

I would like to know if a trigger on a system table of PostgreSQL can be executed when I create a table
I need to add 2 functions on each table of my database and I would like to do it dynamically
Thanks
This can be done with an event trigger:
CREATE OR REPLACE FUNCTION on_create_table_func()
RETURNS event_trigger AS $$
BEGIN
-- your code here
END
$$
LANGUAGE plpgsql;
CREATE EVENT TRIGGER
on_create_table ON ddl_command_end
WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE on_create_table_func();
Note that there is no way to directly execute any query on the newly created table, or even get its name.
I don't know what you mean by "add 2 functions on each table" since functions don't belong to a specific table, but if you need to perform an operation specific for the new tables, this might not be for you.
I know it's an old question but now it has been implemented in version 9.3, or at least partially
http://www.postgresql.org/docs/9.3/static/event-trigger-definition.html
You're looking for "DDL Triggers". They're not implemented in PostgreSQL. Neither you can add triggers to system tables. Look at this forum entry:
Adding ddl audit trigger

Learning about DB2 triggers

Hi I am trying to figure out the syntax for triggers. I have two tables one called tagged_in and the other notification. So I want to make a trigger where when an insert is called in tagged_in I want to insert a tuple in notification.
The manuals are available at the DB2 InfoCenter. Did you read the CREATE TRIGGER statement information yet? If not, why not? If so, what did you try, and what error did you get?
Something like this
CREATE TRIGGER TEST_TRIGGER
AFTER INSERT ON TAGGED_IN
REFERENCING NEW AS NEW_TAG
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO NOTIFICATION
VALUES (NEW_TAG.FIELD1,NEW_TAG.FIELD2);
END

postgres trigger creation

How do I only create a trigger if it does not exist?
When I do create or replace, I get a syntax error so I am looking for a way to test for the existence of a trigger.
I can always select * from pg_trigger, but I am sure there is a more suitable way.
Thanks
Postgres can conditionally drop a trigger - see the docs. Do this before creating the trigger, then it will always work.
DROP TRIGGER IF EXISTS mytrigger ON mytable;
As Jack points out in the comments, this feature has only been available since 8.2; this has been out for more than four years though, so it should be available in your version.
CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)
DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);