Sybase ASA 9 Trigger after insert get new row - triggers

By using trigger I want to copy new added record to another table. I use the follow trigger but it is not bringing values from new added row.
ALTER TRIGGER "SalesTRG" after insert on
DBA.SalesTransaction_Archive
REFERENCING NEW AS new_salestransaction
for each row
WHEN ( new_salestransaction.LocCreatedClientNumber = '0001' )
BEGIN
insert into SalesFG(TransactionNumbers) select TransactionNumber from new_salestransaction
END
Thankx in advance

the insert inside your trigger should look like:
insert SalesFG(TransactionNumbers) values(new_salestransaction.TransactionNumber)
Be aware, the trigger will be fired only when you insert new record with LocCreatedClientNumber = '0001'

Related

Trigger that updates stock levels after a sale - MySQL Workbench

I am trying to create a trigger called 'stockupdate' that updates 'quantity_available' when a new order is placed on my order_line_item_table.
Tables and columns required: stock_table.quantity_available (Represents Stock) order_line_item_table.sale_quantity (Represents each order placed)
I have used the below code to create the trigger, however when I insert a new order to check the trigger is working, the stock_table.quantity_available figure does not update and I am not sure why. Any help would be greatly appreciated.
DELIMITER $$
DROP TRIGGER IF EXISTS stockupdate;
CREATE TRIGGER stockupdate
AFTER INSERT ON order_line_item_table
FOR EACH ROW
BEGIN
UPDATE stock_table
SET quantity_available = (quantity_available - NEW.sale_quantity)
WHERE product_id = NEW.product_id;
END$$
DELIMITER ;
I am expecting the quantity_available to decrease by the sale_quantity amount when a new order is placed on the order_line_item_table

Get data of multiple inserted rows in one object using trigger in postgres

I am trying to write a trigger which gets data from the table attribute in which multiple rows are inserted corresponding to one actionId at one time and group all that data into the one object:
Table Schema
actionId
key
value
I am firing trigger on rows insertion,SO how can I handle this multiple row insertion and how can I collect all the data.
CREATE TRIGGER attribute_changes
AFTER INSERT
ON attributes
FOR EACH ROW
EXECUTE PROCEDURE log_attribute_changes();
and the function,
CREATE OR REPLACE FUNCTION wflowr222.log_task_extendedattribute_changes()
RETURNS trigger AS
$BODY$
DECLARE
_message json;
_extendedAttributes jsonb;
BEGIN
SELECT json_agg(tmp)
INTO _extendedAttributes
FROM (
-- your subquery goes here, for example:
SELECT attributes.key, attributes.value
FROM attributes
WHERE attributes.actionId=NEW.actionId
) tmp;
_message :=json_build_object('actionId',NEW.actionId,'extendedAttributes',_extendedAttributes);
INSERT INTO wflowr222.irisevents(message)
VALUES(_message );
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
and data format is,
actionId key value
2 flag true
2 image http:test.com/image
2 status New
I tried to do it via Insert trigger, but it is firing on each row inserted.
If anyone has any idea about this?
I expect that the problem is that you're using a FOR EACH ROW trigger; what you likely want is a FOR EACH STATEMENT trigger - ie. which only fires once for your multi-line INSERT statement. See the description at https://www.postgresql.org/docs/current/sql-createtrigger.html for a more through explanation.
AFAICT, you will also need to add REFERENCING NEW TABLE AS NEW in this mode to make the NEW reference available to the trigger function. So your CREATE TRIGGER syntax would need to be:
CREATE TRIGGER attribute_changes
AFTER INSERT
ON attributes
REFERENCING NEW TABLE AS NEW
FOR EACH STATEMENT
EXECUTE PROCEDURE log_attribute_changes();
I've read elsewhere that the required REFERENCING NEW TABLE ... syntax is only supported in PostgreSQL 10 and later.
Considering the version of postgres you have, and therefore keeping in mind that you can't use a trigger defined FOR EACH STATEMENT for your purpose, the only alternative I see is
using a trigger after insert in order to collect some information about changes in a utility table
using a unix cron that execute a pl/sql that do the job on data set
For example:
Your utility table
CREATE TABLE utility (
actionid integer,
createtime timestamp
);
You can define a trigger FOR EACH ROW with a body that do something like this
INSERT INTO utilty values(NEW.actionid, curent_timestamp);
And, finally, have a crontab UNIX that execute a file or a procedure that to something like this:
SELECT a.* FROM utility u JOIN yourtable a ON a.actionid = u.actionid WHERE u.createtime < current_timestamp;
// do something here with records selected above
TRUNCATE table utility;
If you had postgres 9.5 you could have used pg_cron instead of unix cron...

Simple IBM DB2 Trigger

I'm trying to create a trigger in DB2 (AS400) to insert/delete a row into a table when an insert/delete is triggered on a different table, but I need to use information about the triger table.
The example would be I would like is like this (column 1 from table 1 and table 2 are the same and unique in table 2):
CREATE TRIGGER MY_TRIGGER
AFTER INSERT OR DELETE ON DB1.TABLE1
BEGIN
IF INSERTING
THEN INSERT INTO DB1.TABLE2 (Col1, Col2) VALUES (Db1.TABLE1.Col1, 0);
ELSEIF DELETING
THEN DELETE FROM Db1.TABLE2 WHERE Col1=TABLE1.Col1;
END IF;
END
But this doesn't work (it doesn't recognize TABLE1.Col1 on insert/delete statements).
Also it would prompt me an error (I guess) since it would create a duplicate key when a second row is inserted in Table 1. How could I avoid errors (just skip the insert) when the Table2.Col1 already exists?
Try adding correlation names like this:
CREATE TRIGGER MY_TRIGGER
AFTER INSERT OR DELETE ON DB1.TABLE1
REFERENCING OLD ROW AS OLD
NEW ROW AS NEW
BEGIN
IF INSERTING
THEN INSERT INTO DB1.TABLE2 (Col1, Col2) VALUES (NEW.Col1, 0);
ELSEIF DELETING
THEN DELETE FROM Db1.TABLE2 WHERE Col1=OLD.Col1;
END IF;
END
The trigger has access to both the old and new image of a row. You need to tell it which to use. BTW, only the update action populates both the old and new image. Insert only provides the new image, and delete only provides the old image. One might think that SQL could figure that out, but no, you still have to tell it explicitly.
EDIT This is the final trigger actually used (from comments, thank you #MarçalTorroella)
CREATE TRIGGER MY_TRIGGER
AFTER INSERT OR DELETE ON DB1.TABLE1
REFERENCING OLD ROW AS OLD
NEW ROW AS NEW
FOR EACH ROW MODE DB2ROW
BEGIN
DECLARE rowcnt INTEGER;
IF INSERTING THEN
SELECT COUNT(*)
INTO rowcnt
FROM DB1.TABL2
WHERE Col1 = NEW.Col1;
IF rowcnt = 0 THEN
INSERT INTO DB1.TABLE2 (Col1, Col2)
VALUES (NEW.Col1, 0);
END IF;
ELSEIF DELETING THEN
DELETE FROM Db1.TABLE2
WHERE Col1=OLD.Col1;
END IF;
END

Trigger before insert in MariaDB

I'm having some problem to create a new trigger before insert a new row.
It should act before insert to stop an insert of a new row that has a value that is already referenced from another row in the same table.
I tried to use this trigger but it is not compatible with mariaDB, in fact it gives me asyntax error on referencing.
CREATE TRIGGER BadgeAlreadyUsed
BEFORE INSERT ON User
REFERENCING NEW AS N
FOR EACH ROW
WHEN (EXISTS ( SELECT IDBadge FROM User WHERE N.IDBadge = User.IDBadge ))
SIGNAL SQLSTATE '70002' ('Badge already used!!');
How i can do the same thing with the new syntax?
thanks.
Each database (DB2, MariaDB, etc) has hundreds of differences. Don't assume anything!
This might be closer:
CREATE TRIGGER BadgeAlreadyUsed
BEFORE INSERT ON User
FOR EACH ROW
BEGIN
IF (EXISTS ( SELECT IDBadge FROM User
WHERE NEW.IDBadge = User.IDBadge ))
THEN
SIGNAL SQLSTATE '70002'
SET MESSAGE_TEXT = 'Badge already used!!';
END IF;
END;
Notice there there are at least 3 syntax changes (WHEN, NEW, SET).

Applying trigger on sqlite database in objective-c

How can I apply a trigger on my sqlite database using objective-c.
I want to apply trigger to check if a table has performed an insert operation.
Run a CREATE TRIGGER statement on it. You'll need to find some way to check the trigger fires, so if you e.g. make the trigger insert rows into another table, you can regularly check the table to see if it has new rows.
If you have a table named CSAppointment, the you can write:
ALTER TABLE "CSAppointment" ADD COLUMN "tableUid" INTEGER;
CREATE TRIGGER log_insert AFTER INSERT ON CSAppointment
BEGIN
INSERT INTO CSRowChanges(tableUId, rowUid, deleteFlag ) VALUES (NEW.tableUid, NEW.uid, 0 );
END;
CREATE TRIGGER log_update AFTER UPDATE ON CSAppointment
BEGIN
INSERT INTO CSRowChanges(tableUId, rowUid, deleteFlag) VALUES (NEW.tableUid, NEW.uid, 0) ;
END;
CREATE TRIGGER log_delete AFTER DELETE ON CSAppointment
BEGIN
INSERT INTO CSRowChanges(tableUId, rowUid, deleteFlag) VALUES (OLD.tableUid, OLD.uid, 1 ) ;
END;
This way you are adding triggers for insert, update and delete operations.
More information on here.