Trigger Specific value into different database - sql

i am new to trigger, is there a way to trigger input to databaseB.track conferenceid whenever there is a new record in databaseA.conference? this is what i did and is not working.
DELIMITER//
CREATE or replace TRIGGER insert_confer
after insert ON conference
for each row
begin
insertdatabaseb.Track(:new.conferenceid);
end;
/
DatabaseA Database B
Conference Track
-conferenceid -Trackid
-conferencename -Trackname
-conferencevenue -Conferenceid

The issue isn't the trigger per se but rather the syntax on the insert. Perhaps:
DELIMITER//
CREATE or replace TRIGGER insert_confer
after insert ON conference
for each row
begin
insert into databaseb.Track(conferenceid)
values (:new.conferenceid);
end//
DELIMITER ;

Related

How to select all inserted rows to execute an insert trigger with a stored procedure in postgresql?

I'm trying to set an "after insert" trigger that executes a procedure. The procedure would take all inserted rows in table A, group them by a column and insert the result in a table B. I know about "new" variable but it gets inserted rows one by one. Is it possible to get all of them?
I think I can't use a for each row statement as I need to group rows depending on the "trackCode" variable, shared by different rows in tableA.
CREATE OR REPLACE PROCEDURE Public.my_procedure(**inserted rows in tableA?**)
LANGUAGE 'plpgsql'
AS $$
BEGIN
INSERT INTO Public."tableB" ("TrackCode", "count")
SELECT "TrackCode", count(*) as "count" FROM Public."tableA" --new inserted rows in this table
GROUP BY "vmsint"."TrackCode" ;
COMMIT;
END;
$$;
create trigger Public.my_trigger
after insert ON Public.tableA
execute procedure Public.my_procedure(**inserted rows in tableA?**)
Thank you!
You create a statement lever trigger, but do not attempt to pass parameters. Instead use the clause referencing new table as reference_table_name. In the trigger function you use the reference_table_name in place of the actual table name. Something like: (see demo)
create or replace function group_a_ais()
returns trigger
language 'plpgsql'
as $$
begin
insert into table_b(track_code, items)
select track_code, count(*)
from rows_inserted_to_a
group by track_code ;
return null;
end;
$$;
create trigger table_a_ais
after insert on table_a
referencing new table as rows_inserted_to_a
for each statement
execute function group_a_ais();
Do not attempt to commit in a trigger, it is a very bad id even if allowed. Suppose the insert to the main table is part of a larger transaction, which fails later in its process.
Be sure to refer to links provided by Adrian.

trigger to update specific column when insert/update happened in same table

I trying to write a trigger that will update a column when user insert or updates a row, within the same table.
Example:
insert into user(ID, F_NM, L_NM, EMAIL) values ('1', 'John','Doe','john.doe#market.org.com');
after the insert, i want to call: update user set ORG = 'market' where ID = '1'.
create or replace trigger user_change
after insert or update of EMAIL on USER
for each row
declare
NEW_ORG VARCHAR(10);
BEGIN
CASE
when :NEW.EMAIL like '$#market.org.com' then
NEW_ORG := 'market';
........
END CASE;
UPDATE USER set ORG = NEW_ORG where ID = :NEW.ID
END;
Calculating the new ORG work, but I can't get the update statement to work.
I get 'ORA-04091 table USER is mutating, trigger/funtion may not see it', figure its due to me inserting/updating the same record at same time. Tried adding 'pragma autonomous_transaction' and 'commit' to the trigger, the insert/update of fields works but the ORG does not get updated.
Also tried changing to INSTEAD OF INSERT OR UPDATE OF EMAIL but I keep getting 'ORA-04073 column list not valid for this trigger type'
create or replace trigger user_change
instead of insert or update of EMAIL on USER
while i get 'ORA-25002 cannot create instead of triggers on tables'
create or replace trigger user_change
instead of insert on USER
Why not simply turn the trigger to a before trigger, when you can set the value before it is written? This way, you don't need to run a new DML statement on the table, which avoid the "mutating" error.
create or replace trigger user_change
after insert or update of email on user
for each row
begin
if :new.email like '%#market.org.com' then
:new.org := 'market';
end if;
end;
Looks like your org column can be calculated virtual column. In this case it would be better to create user-defined deterministic pl/sql function that returns correct calculated value and add it to your table, for example:
Alter table t add org varchar2(30) generated always as (f_get_org(email))

ORACLE SQL: After Update Trigger

Problem Statement:
Create a trigger named trigger_contact_af_update that is triggered whenever the contact table is updated. This trigger will insert the org_name and action into the table contact_log_history after the update of contact details. The action name in the affected log table contact_log_history is After_Update_Contact.
The query I have at the moment is:
CREATE OR REPLACE TRIGGER trigger_contact_af_update
AFTER UPDATE ON contact
FOR EACH ROW
BEGIN
INSERT INTO contact_log_history (org_name, action) Values (:OLD.org_name, 'After_Update_Contact');
END;
But it's not working. Can someone please tell me why?
The table contact_log_history has only two columns.
Using the :OLD variable only works when using a before trigger. I would recommend you to do that in your case.
CREATE OR REPLACE TRIGGER trigger_contact_af_update
BEFORE UPDATE ON contact
FOR EACH ROW
BEGIN
INSERT INTO contact_log_history (org_name, action) Values (:old.org_name, 'After_Update_Contact');
END;

SQL Trigger to Insert a row

I'm using postgresql. These are the table involved in the trigger:
Pricing (title,publisher,period,offer,price)
The DB has 4 tables describing a magazine business.
The table above shows the title, publisher of the magazine, period (an option the subscribe to the magazine, in months (integer), offer (a string, like 'regular' or 'renew'), and price (integer).
The question:
Write a trigger that adds a new row in the pricing table.
If a new row is inserted with offer='regular', insert a new row, exactly the same BUT:
offer='renew' and a price discount of 10%.
Here my trigger, which doesn't work:
create or replace function offer_f() returns trigger as $$
begin
if(TG_OP='insert') then
if new.offer='regular' then
insert into pricing (title,pusblisher,offer,period, price)
values (new.title,new.publisher,"renew",new.period,new.price*0.9);
end if;
end if;
return new;
end;
$$
language plpgsql;
This is the trigger itself:
create trigger reduced_offer
after insert or update on pricing
for each row
execute procedure offer_f()
I really don't know what's wrong here, and I have tried replacing "after" with "before" in the trigger.
Thanks,
Alan

How to get last inserted row in sap hana sql trigger?

I am creating this sql trigger in SAP HANA and cannot figure out how to reference the last inserted row, here is the code for the trigger:
CREATE TRIGGER MY_TRIGGER
AFTER INSERT ON TARGET_TABLE
FOR EACH ROW
BEGIN
CALL SOME_PROCEDURE(:NEWROW.id);
END;
I tried the REFERENCING NEW ROW syntax but it seems that it is not supported for INSERT AFTER triggers.
Help!
CREATE TRIGGER TEST_TRIGGER_VAR_UPDATE
AFTER UPDATE ON TARGET
REFERENCING NEW ROW mynewrow, OLD ROW myoldrow
FOR EACH ROW
BEGIN
INSERT INTO SAMPLE_new VALUES(:mynewrow.a, :mynewrow.b);
INSERT INTO SAMPLE_old VALUES(:myoldrow.a, :myoldrow.b);
INSERT INTO SAMPLE VALUES(0, 'trigger');
END;