Delete before delete trigger - sql

I have two tables:
Table MASTER
Table SLAVE with a column ID that always matches a MASTER.ID
I would like to delete the SLAVE before deleting a MASTER through a DB2 trigger.
Here's what I wrote:
CREATE TRIGGER delete_slave
BEFORE DELETE ON MASTER
REFERENCING OLD AS o FOR EACH ROW
DELETE FROM SLAVE WHERE ID = o.ID;
When I execute the query on a DB2 V10.5.0.3 database I get the following error:
The trigger "SCHEMA1.DELETE_SLAVE" is defined with an unsupported
triggered SQL statement.. SQLCODE=-797, SQLSTATE=42987, DRIVER=4.17.29
SQL Code: -797, SQL State: 42987
Could someone help me to identify what's wrong in the SQL statement that's triggered ?
Thank you a lot in advance.

OK I found the answer,
According to http://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000931.html:
The SQL-procedure-statement in a BEFORE trigger cannot: Contain any
INSERT, DELETE, or UPDATE operations, nor invoke any routine defined
with MODIFIES SQL DATA, if it is not a compound SQL (compiled).

Related

PL/SQL Replicating a table with a trigger on Oracle DB

I have never used triggers in PLSQL before, but I am now supposed to make a trigger that replicates the table of one database, and creates a copy of this table in another database. I am using AQT(Advanced Query Tool) as DBMS, and i have 2 database connections, and I need to copy the table and or data from DB1 to DB2. It's only based on one table that I need replicated, following tutorialspoint I have concluded that it should look somewhat like this:
`
CREATE OR REPLACE TRIGGER db_transfer
AFTER DELETE OR INSERT OR UPDATE ON X
WHEN (NEW.ID > 0)
I don't think i need for each since i want a copy of the whole table, and the condition is supposed to trigger this replication of the DB table. Is this the right approach?
EDIT
For anyone who uses AQT, they have a feature under Create -> Trigger -> and then click on the relevant tables etc to create it.

How to create a trigger to delete a record

Looking for ideas on how to write a trigger that will delete a record when a condition is met.
I have some records being written to a SQL Server 2008 database table. These records that are being written are not important and I would like to see them removed. There are two columns that will have unique information when these records are written and I would like a trigger to remove them as soon as they are written.
I could run a daily job to delete all records that equal X and Y but my thought why not delete the records as they are written.
What would this kind of trigger look like?
If you are not able to change that at any app layer you can use an INSTEAD OF INSERT trigger and those rows will not even be inserted at all.
It will save the database from both inserting and deleting.

one sql trigger insert,update,delete for all tables in the database

is it possible to declare one Sql Trigger Insert, Update, Delete for all tables in the database instead of creating a separate trigger for each table? I just want a simple history of what actions have been taken e.g. TABLE A deletes a row ,TABLE B updates a row ,TABLE C Add new row .. and the trigger will insert a new row to another table with that information I want.
No, a trigger can only be defined on a specified table.
You should read up on the auditing features of SQL Server (http://technet.microsoft.com/en-us/library/cc280386.aspx). They are more performant and more flexible in what you want to achieve. Unfortunately they are not available in the Express Edition.

Need a sql statement to do upate and insert at the same time

I need a sql statement, to insert a new row in one database table and update an existing row in another database table based on some conditions.
Is there a way to do this? To insert a row in one table and update a row in another database table in one sql statement?
Thanks in advance!
Yes, they are called Transactions, and are implemented with START TRANSACTION and COMMIT/ROLLBACK with something like:
START TRANSACTION;
INSERT INTO ...
UPDATE table2 SET name='TOTO' WHERE type=1;
COMMIT;
EDIT
This is not in fact one SQL query, but the operation is done atomically - and I think that is what you need.
A single SQL statement allows you to update one table, not several; if that statement is a MERGE then you can specify insert/update/delete actions but still targeting just the same one target table.
If you just want consistency, use transactions; until a transaction is committed, changes within it are not visible to the outside world.
If you want that a single update (which you cannot control) resulted in a coordinated insert, use an on update trigger in the table being updated. The trigger would insert appropriate row(s) into other table(s).
You can use Trigger to update second table on insert of first table
Yes, it's possible with stored procedures.
Watch this: Stored procedures

sql server 2005 trigger after delete

I have written a trigger on a table after delete row to update some other table using a field from the 'deleted' (magic table). It works fine for single row delete.
But when I try to do deletes like
delete from tblIndentDetails
WHERE indentID=5;
(it is supposed to delete more than one records meeting the criterion), the delete operation is not performed due to trigger. How can I work around the situation?