how to fix this trigger error in PostgreSQL [duplicate] - sql

This question already has answers here:
syntax Error in PostgreSQL when I try to create Trigger
(2 answers)
Closed last year.
I am getting a syntax error for my code which I can't understand why
am I missing something?
also, I read this I did not get my answer
syntax Error in PostgreSQL when I try to create Trigger
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
and tried it like this as well:
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW AS $$ BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
$$ LANGUAGE plpgsql;
Error:
ERROR: syntax error at or near "BEGIN"
LINE 2: FOR EACH ROW BEGIN
^
SQL state: 42601
Character: 67

I'd say the first comment on your question pretty much covers it all. You cannot put the trigger code in the trigger body, you must first create a separate function and include the function call inside the trigger body.
This example comes directly from the Postgres docs:
-- 1. Create the function that does what you need
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when they must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql;
-- 2. Create the trigger with the 'EXECUTE FUNCTION function_name' part
-- replacing the actual function name from step 1.
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE FUNCTION emp_stamp();

Related

How can I use 'BEFORE DELETE' trigger with conditions

colleagues! I created 'product' table with the next query:
CREATE TABLE product (
id serial not null,
product_name text not null,
description varchar(50),
delivery_date timestamp,
warehouse jsonb
)
And I'm trying to use trigger before delete with the sort by list:
CREATE OR REPLACE FUNCTION product_delete_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF (OLD.product_name IN ('Milk','Egg','Cheese'))
THEN
DELETE FROM product WHERE product_name = OLD.product_name;
ELSE
RAISE EXCEPTION
'Value out of list. Fix the product_delete_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER delete_product_trigger
BEFORE DELETE ON product
FOR EACH ROW EXECUTE PROCEDURE product_delete_trigger();
How I got it I need to use 'OLD' special parameter , but If I when I use it, I have an issue:
ERROR: ERROR: Stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (the current value is 2048 KB), first making sure that the OS provides sufficient stack size.
Is it possible to do this by this query?
DELETE FROM product where product_name = 'Cheese';
There is no point in running DELETE in your BEFORE trigger – that happens anyway if you let the database have its course. So all you have to do is throw an error if you are unhappy with the proceedings:
CREATE OR REPLACE FUNCTION product_delete_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ((OLD.product_name IN ('Milk','Egg','Cheese')) IS NOT TRUE)
THEN
RAISE EXCEPTION
'Value out of list. Fix the product_delete_trigger() function!';
END IF;
/* proceed with the DELETE */
RETURN OLD;
END;
$$
LANGUAGE plpgsql;

Error creating trigger in Oracle PLSQL

I'm a student learning SQL in Oracle. I'm creating a trigger and within the trigger I'm calling a procedure update_price_history(item_id, NEW.price).
This is the statement I did,
CREATE OR REPLACE TRIGGER upd_hist_trg
BEFORE INSERT OR UPDATE OF price ON Item
FOR EACH ROW
BEGIN
IF :NEW.price != price THEN
update_price_history(item_id, NEW.price)
END IF;
END;
I thought I had everything correct but I'm getting an error message:
Error(3,2): PLS-00103: Encountered the symbol "END" when expecting one of the following: := . ( % ; The symbol ";" was substituted for "END" to continue.
I've been trying to figure out what I'm getting wrong and I'm not seeing what it is. Any help would be greatly appreciated.
You forgot the semicolon at the end
...
update_price_history(item_id, :NEW.price);
...
Try this :
CREATE OR REPLACE TRIGGER upd_hist_trg
BEFORE INSERT OR UPDATE OF price ON Item
FOR EACH ROW
DECLARE
item_id number;
BEGIN
IF :NEW.price != price THEN
update_price_history(item_id, :NEW.price);
END IF;
END;
you forgot : in front of new and a semicolon ; after procedure. By the way, i can't see any variable definition of item_id.

Error: ORA-04072: invalid trigger type

I did the following trigger:
create or replace trigger check_club_location
before of insert or update on club
for each row
declare cnt number;
begin
select count(*) into cnt
from club
where club.location = :new.location;
if (cnt > 0) then
raise_application_error (-20100, 'Erro: ja existe club nessa localizacao.');
end if;
end;
/
After I tried to run this trigger in Oracle APEX, the following error message appears:
ORA-04072: invalid trigger type.
I already tried many fixes but none worked on my trigger unfortunatelly.
You have a typo
before of insert or update on club
remove the "of", Oracle is complaining that "of" is not a valid trigger type

PL SQL trigger using raise_application_error thows error.

I have a few things of code I need help debugging but I feel that if I can get one of them running i'll be able to get the rest(oh how i hope).
create or replace
trigger minimumwage
before insert or update on Employee
for each row
begin
if :new.Wage < 7.25
then raise_application_error('-20000,Pay is below Texas minimum wage!');
end if;
end;
/
I'm trying to do this on a table ran on my school's server through sqlplus if that helps.
When you're getting an error, it's always helpful to specify what error. There is a syntax error in the raise_application_error call in your trigger. That procedure takes two arguments, a number and a string. You are passing in a single argument that is one long string.
create or replace trigger minimumwage
before insert or update on Employee
for each row
begin
if :new.Wage < 7.25
then
raise_application_error(-20000,'Pay is below Texas minimum wage!');
end if;
end;
should be valid assuming there is a WAGE column in your EMPLOYEE table.
create or replace trigger deny_dec_pu before update of PU on ARTICLE
for each row
declare
erreur_pu exception;
begin
*insert into erreur values ('Operation de MAJ',sysdate);*
-- this intruction is never executec why ?
if (:new.pu < :old.pu) then
raise erreur_pu ;
end if;
exception
when erreur_pu then
Raise_application_error(-20100, 'rrrrrrrr', FALSE);
end;
/

Oracle SQL trigger results in compilation error

I have a problem with trigger not working. Here is my trigger code
create or replace trigger "ZIVOTINJE_T2"
BEFORE
insert or update on "ZIVOTINJE"
for each row
begin
IF new.cijena>10 THEN
:new.cijena:=9.9
ELSEIF new.cijena<0 THEN
:new.cijena:=0.1
END IF;
end;
When I try to insert entity in table ZIVOTINJE, I get this
ORA-04098: trigger 'DENISS.ZIVOTINJE_T2' is invalid and failed re-validation
I can see three problems in the code of your trigger:
You need to refer to the new values of the row using :new (including the colon), not new.
To change values that are about to be inserted/updated, write :new.cijena := 9.9; instead of SET new.cijena=9.9. Note that (a) there is no SET keyword here; (b) the assignment operator is :=, not =; and (c) you need a semi-colon at the end of the line.
Use ELSIF instead of ELSEIF.
Finally, in SQL*Plus, you can use SHOW ERRORS TRIGGER "ZIVOTINJE_T2" to show the errors for this trigger.
try this.
CREATE OR REPLACE TRIGGER "ZIVOTINJE_T2"
BEFORE
insert or update on "ZIVOTINJE"
for each row
begin
IF :new.cijena = 10 THEN
:new.cijena := 9.9;
ELSE
IF :new.cijena < 0 THEN
:new.cijena := 0.1;
END IF;
end if;
end;