Updates/inserts not working in postgres trigger function - sql

I have following function and trigger-
CREATE OR REPLACE FUNCTION function_copyX() RETURNS trigger AS
$BODY$
BEGIN
WITH
updates (id) AS (
UPDATE tbl_history
SET price=0
FROM tbl
WHERE tbl.id = tbl_history.id
AND tbl_history.price=1
RETURNING tbl_history.id
)
INSERT INTO tbl_history
SELECT id,1 FROM tbl;
RETURN new;
END;
$BODY$
language plpgsql;
create TRIGGER T_X
AFTER INSERT ON TBL
FOR EACH ROW
execute procedure function_copyX();
So, whenever I am inserting any record in TBL, I expect inserts/updates in the TBL_HISTORY but this does not work in trigger function.
But if I execute this 'WITH code' separately it works.
What could be the issue? please help.
Thanks

Below modification worked!
CREATE OR REPLACE FUNCTION function_copyX() RETURNS trigger AS
$BODY$
BEGIN
WITH
updates (id) AS (
UPDATE tbl_history
SET price=0 WHERE id =NEW.id AND price=1
RETURNING id
)
INSERT INTO tbl_history (id,price) values (NEW.id,1);
RETURN new;
END;
$BODY$
language plpgsql;
create TRIGGER T_X
AFTER INSERT ON TBL
FOR EACH ROW
execute procedure function_copyX();

Related

How To Write a trigger in postgres to update another column when data inserted into another cloum with same value in same table?

Please Find below Table structure:-
CREATE TABLE emp (
empname text NOT NULL,
salary integer,
salary1 integer
);
Whenever i will insert data into emp table using below query salary1 column should be filled automatically using trigger in postgres ?
insert into emp(empname,salary) values('Tarik','1200');
I have tried below code,But it is not working for me.
CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
RETURNS trigger AS
$$
BEGIN
update emp set salary1=NEW.salary where NEW.salary=NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
create TRIGGER employee_insert_trigger
AFTER Insert
ON emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();
Please help..
Thanks in advance.
Issue resolved after my R&D:-
CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
RETURNS trigger AS
$$
BEGIN
NEW.salary1 := NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
create TRIGGER employee_insert_trigger
BEFORE INSERT OR UPDATE
ON ami_master.emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();

Create trigger in Postgres

I have two tables A and B. A has the following columns :
a_name, a_num, a_addr. B also has these columns.
Whenever I insert a record in A, I want that record to be inserted in B as well by the use of a trigger.
CREATE OR REPLACE FUNCTION trigger()
RETURNS trigger AS
$BODY$
begin
if tg_op='INSERT' then
insert into b values (new.a_name ,new.a_num ,new.a_addr);
return new;
end if;
return null;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
---------------------------
CREATE TRIGGER trigger_a
AFTER INSERT OR UPDATE
ON a
FOR EACH ROW
EXECUTE PROCEDURE trigger();

Correct SQL table alias syntax in trigger with if statement

So basically I'm trying to create a trigger that will update another table if an insert or update is located less than 1 meter of distance of an existing one.
It does not allow me to insert anything because the FROM clause is missing on the if statement and I'm not sure how to call it properly.
I got the following
CREATE TRIGGER access_node_aiu
AFTER INSERT OR UPDATE
ON schemab.table
FOR EACH ROW
EXECUTE PROCEDURE schemab.trigegr();
CREATE OR REPLACE FUNCTION schemab.trigger()
RETURNS trigger AS
$BODY$
BEGIN
if st_distance(new.geom, a.geom) < 1 then
INSERT INTO schemab.overlap
SELECT nextval('schemab.overlap_id_seq'::regclass), a.node_id, new.node_id, a.date, now(), st_distance(a.geom, new.geom), new.geom
FROM schemab.table a;
end if;
RETURN new;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
How to do this in postgresql?
Change IF into WHERE
CREATE OR REPLACE FUNCTION schemab.trigger()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO schemab.overlap
SELECT nextval('schemab.overlap_id_seq'::regclass), a.node_id, new.node_id, a.date, now(), st_distance(a.geom, new.geom), new.geom
FROM schemab.table a
WHERE st_distance(new.geom, a.geom) < 1;
RETURN new;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

PL/PgSQL creating a conditional trigger on insert

I'm working on my first ever Trigger. When I'm doing an INSERT on table I want to conditionaly remove rows from other table.
Here is a trigger:
CREATE OR REPLACE FUNCTION clear_seen_by()
RETURNS trigger AS
$BODY$
BEGIN
IF (OLD.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Invoked by:
CREATE TRIGGER clear_seen_by
AFTER INSERT
ON news
FOR EACH STATEMENT
EXECUTE PROCEDURE clear_seen_by();
As an error I see that NEW or OLD (if I motify the trigger) is not declared/unknown. Where is the problem?
In an INSERT statement you do not have an OLD record defined.
You should use NEW.popup instead, and also declare the trigger to be FOR EACH ROW.
CREATE OR REPLACE FUNCTION clear_seen_by() RETURNS trigger AS
$BODY$
BEGIN
IF (NEW.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
CREATE TRIGGER
clear_seen_by
AFTER INSERT ON
news
FOR EACH ROW EXECUTE PROCEDURE
clear_seen_by();
You declare a trigger FOR EACH STATEMENT. Maybe you need FOR EACH ROW?
FOR EACH STATEMENT triggers do not have NEW and OLD.

Trigger Not Executing Yet It's Created

I have a trigger function I'm trying to have execute in Postgres.
It compiles and adds the trigger, however it does not insert the value into the table as I had hoped.
The function it uses looks like this:
CREATE OR REPLACE FUNCTION
calc_gnpDifference(n integer, o integer)
RETURNS NUMERIC AS $$
SELECT $1 ::numeric - $2::numeric AS gnpDifference;
$$ LANGUAGE SQL;
And the Trigger part:
CREATE OR REPLACE FUNCTION autoCalculate() RETURNS TRIGGER AS $$
BEGIN
IF NEW.gnp_old > NEW.gnp_old THEN
NEW.gnpDifference := calc_gnpDifference(NEW.gnp_old, NEW.gnp);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER insertDifference ON country;
CREATE TRIGGER insertDifference BEFORE INSERT ON country
FOR EACH ROW EXECUTE PROCEDURE autoCalculate();
However, when I insert data, the trigger does not update the gnpDifference field as I had hoped. Thoughts on why this might be happening?
Obviously this condition: IF NEW.gnp_old > NEW.gnp_old will never be true so the trigger will never have any effect.