i am getting compilation error each time i tried to create a trigger: please find below problem statement and code:
problem Statement:
Create a trigger named 'trigger_credit_bf_update' that is triggered whenever the credit_card table is updated. This trigger will insert the cc_type and action into the table 'credit_card_log_history' before the updation of credit_card details. The action name in the affected log table credit_card_log_history is 'Before_Update_Credit_Card':
my code link this for question:
Code:
create or replace trigger trigger_credit_bf_update
before update on credit_card
for each row;
BEGIN
insert into credit_card_log_history (cc_type, action)
values (:old.cc_type, 'Before_Update_Credit_Card');
END;
CREATE OR REPLACE TRIGGER trigger_credit_bf_update
BEFORE UPDATE ON credit_card
FOR EACH ROW
BEGIN
INSERT INTO credit_card_log_history (cc_type, action) Values
(:old.cc_type, 'Before_Update_Credit_Card');
END;
Related
i am creating a trigger that is supposed to be fired every time a new device is added in to the device table. Since the id in the table is auto incrementing i have set is so that the trigger is fired after the insert. The insertion works but the trigger is not being fired or atleast i am not seeing the data in the table that is supposed to receive the information.
the function
CREATE OR REPLACE function insertstagingdevice() returns trigger as $device$
DECLARE
begin
INSERT INTO postgres.staging_import_remote_access.device(device_id,
name, eui,
location)
VALUES(new.id,
new.name,
new.eui,
new.location);
RETURN NEW;
END
$device$ language plpgsql;
The trigger
CREATE TRIGGER insertionIntoDevice
after INSERT
ON postgres.public.device
FOR EACH row
EXECUTE PROCEDURE insertstagingdevice();
Is there something i am overseeing ?
The code is correct. there was an error elswehere. Thanks for your help
I created a table name Student in PostgreSQL and then I tried defining a trigger on that table but it's showing an error message in doing so.
Trigger Syntax:
CREATE TRIGGER bi_Student BEFORE INSERT ON Student as $$
FOR EACH ROW
BEGIN
raise notice 'Successfully inserted into table(%)', user;
end $$;
Table Creation Command:
create table Student(Stu_id int, Stu_Name text, Stu_Age int, Stu_address char(30));
Actually I tried to declare the execution statements directly inside the trigger only rather than calling any procedure/ function from the trigger which is working fine but I want to do in this way in PostgreSQL.
PostgreSQL doesn't support it. You need trigger function always.
As documented in the manual you need a trigger function
create function my_trigger_function()
returns trigger
as
$$
begin
raise notice 'Successfully inserted into table(%)', user;
return new; --<< important (see the manual for details)
end
$$
language plpgsql;
Not sure what you intend with the user parameter there, as that is not the table name, but the current database user. If you want to display the actual table name, you need to use TG_RELNAME instead - which is an implicit variable available in the trigger function.
And a trigger definition
CREATE TRIGGER bi_Student
BEFORE INSERT ON Student
FOR EACH ROW
execute function my_trigger_function();
I have created the following trigger;
CREATE TRIGGER material_trigger
BEFORE INSERT OR UPDATE ON materials
FOR EACH ROW
EXECUTE PROCEDURE my_proc ();
I'm struggling to understand when my_proc will be run on an upsert query, such as the below:
--UPSERT
INSERT INTO materials (id, col)
VALUES (1, 1)
ON CONFLICT (id) DO UPDATE SET
col='x'
If I'm running the upsert, am I right to think that:
in the case of an INSERT it will be run once
in the case of an UPDATE it will be run twice
Then logically if I ran this AFTER the upsert I would ensure that the my_proc was triggered only once?
The trigger is, infact fired twice in the case that the UPDATE is carried out. This can be demonstrated using the follwing:
-- create table
CREATE TABLE IF NOT EXISTS example (uuid int unique);
-- create function
CREATE OR REPLACE FUNCTION print_function ()
RETURNS TRIGGER
AS $body$
BEGIN
RAISE NOTICE 'Starting';
RAISE NOTICE 'METHOD: %' , TG_OP;
RETURN NEW;
END;
$body$
LANGUAGE plpgsql;
-- create trigger function
CREATE TRIGGER example_trigger
BEFORE INSERT OR UPDATE ON example
FOR EACH ROW
EXECUTE PROCEDURE print_function ();
-- upserts
INSERT INTO example(uuid) VALUES (1) ON CONFLICT(uuid) DO UPDATE SET uuid=5;
INSERT INTO example(uuid) VALUES (1) ON CONFLICT(uuid) DO UPDATE SET uuid=5;
which gives output:
NOTICE: Starting
NOTICE: METHOD: INSERT -- Populated with inital value
NOTICE: Starting
NOTICE: METHOD: INSERT -- Fired a first time for insert (can't insert as exists)
NOTICE: Starting
NOTICE: METHOD: UPDATE -- Fired a second time for update
INSERT 0 1
I have a table with three columns: id, date and dateDekete
I try to execute an update on the column dateDelete after an update on another column (column date) using a AFTER UPDATE TRIGGER.
The code that I use to create my trigger is the following:
CREATE OR REPLACE FUNCTION update_delete_date_allocation()
RETURNS trigger LANGUAGE plpgsql AS $body$
BEGIN
NEW."dateDelete" := NEW.date + 1;
RETURN NEW;
END;
$body$;
CREATE TRIGGER delete_date_allocation_trg
AFTER INSERT OR UPDATE ON client.client_portfolio_allocation
FOR EACH ROW
EXECUTE PROCEDURE update_delete_date_allocation();
Although the code executes fine with no error message, the latter column that I try to update does not change.
I was wondering if it's possible to do this. AND if so, what should I change in my code?
I am using Postgres 11.5.
you can't change the new record in an AFTER trigger, you need to declare your trigger as a BEFORE trigger:
CREATE TRIGGER delete_date_allocation_trg
BEFORE INSERT OR UPDATE ON client.client_portfolio_allocation
FOR EACH ROW
EXECUTE PROCEDURE update_delete_date_allocation();
I'm trying to create a function + trigger that will update my "modif" attribut to current date when there is an update or insert on my table called "nada".
the code work well but all the rows are affected.I only want the current date on the rows that were updated.
Any idea ?
This is my code so far:
CREATE OR REPLACE FUNCTION public.maj_modif()
RETURNS "trigger" AS
$BODY$
BEGIN
NEW.modif:= (SELECT current_date);
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS maj_modif ON public.nada;
CREATE TRIGGER maj_modif BEFORE INSERT OR UPDATE ON public.nada
FOR EACH ROW
EXECUTE PROCEDURE public.maj_modif();
If I try the same code without "FOR EACH ROW" in the trigger I get this erreur: « new » is not affected yet (...) The structure of the registration line is not yet determined.
I assume that you only want the trigger to fire if any columns were actually changed.
That can be done with
CREATE TRIGGER maj_modif BEFORE UPDATE ON public.nada
FOR EACH ROW
WHEN OLD <> NEW
EXECUTE PROCEDURE public.maj_modif();
That only works for UPDATE, because on INSERT OLD is not defined. Define the INSERT trigger without the WHEN clause.