I'm trying to create a trigger that multiplies that salaries of employees who're paid more than 1000$ by 2 but it displaying me this error below, here's the code. I'm still discovering this "new/old" concept. But I don't see why my trigger isn't working. Does anybody know why?
CREATE OR REPLACE TRIGGER MAJ
AFTER UPDATE on EMPLOYEES
FOR EACH ROW
begin
IF (:new.salary <1000) THEN
:new.salary:= :new.salary*2;
end;
/
You are forgetting an END IF in your code -
CREATE OR REPLACE TRIGGER MAJ
AFTER UPDATE on EMPLOYEES
FOR EACH ROW
BEGIN
IF (:new.salary <1000) THEN
:new.salary := :new.salary*2;
END IF;
END;
/
Related
I`m trying to solve a trigger issue. So I have to replace the value for every inserted row with new value from the sequence DEPARTMENTS_SEQ. And I ended up here
create or replace TRIGGER hr_insert_tr on DEPARTMENTS
BEFORE INSERT on DEPARTMENTS
for each ROW
BEGIN
DEPARTMENT_ID = :new.DEPARTMENTS_SEQ;
END;
That would be
create or replace trigger hr_insert_tr
before insert on departments
for each row
begin
:new.department_id := departments_seq.nextval;
end;
/
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();
I am trying to create a simple trigger in sql developer to display the change in salary when it is changed
CREATE OR REPLACE TRIGGER salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON FACULTY
FOR EACH ROW
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.F_SALARY - :OLD.F_SALARY;
DBMS_OUTPUT.PUT_LINE('Difference: ' || sal_diff);
END;
when I attempt to run the trigger it prompts be to enter binds for NEW and OLD and when i try run an update to see if it works it states the trigger failed. So how am i using the old and new tags incorrectly? or is that not the issue
There are several issues with your code.
You need to create a after trigger instead of the before trigger.
You are trying to write a trigger that performs an operation for
insert,delete or update. So you should do the conditional checks
(such as if inserting,deleting or updating) clause.
Also, when you do delete, there is no new value, but just the old
value.
I would change your trigger as below..
CREATE OR REPLACE TRIGGER salary_changes
AFTER DELETE OR INSERT OR UPDATE ON FACULTY
FOR EACH ROW
DECLARE
sal_diff NUMBER;
BEGIN
If (INSERTING or UPDATING) then
sal_diff := :NEW.F_SALARY - :OLD.F_SALARY;
DBMS_OUTPUT.PUT_LINE('Difference: ' || sal_diff);
END IF;
IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('The deleted value is:' || :OLD.F_SALARY);
END IF;
END;
I cant find the right structure for the following question in pl/sql:
need a trigger on “products table” that will check the price before inserting a new product, product price must not exceed 4000$.
CREATE or REPLACE TRIGGER pro
BEFORE UPDATE OF price
ON products
FOR EACH ROW
declare
pr products.price%type;
BEGIN
if pr < 4000 then
INSERT INTO products VALUES (:old.product_ID,:old.price);
end if;
END;
Please help
Use check constraint instead of trigger:
create table products (price number);
ALTER TABLE PRODUCTS ADD CONSTRAINT check_price CHECK (price < 4000);
Test:
insert into products values (5000) => ERROR
Edit: If you insist on trigger version:
CREATE or REPLACE TRIGGER pro BEFORE insert or UPDATE OF price ON products
FOR EACH ROW
BEGIN
if :new.price > 4000 then
raise_application_error(-20101, 'Price exceeds 4000.');
end if;
END;
The only reason you would bother with a trigger for this instead of a check constraint is to control the error message. And remember that a trigger assumes that the operation is happening, so to stop the operation your tool is to raise an exception.
CREATE OR REPLACE TRIGGER pro
BEFORE INSERT OR UPDATE
ON products
FOR EACH ROW
BEGIN
if :new.price > 4000 then
RAISE_APPLICATION_ERROR(-20001,'Price exceeds maximum permitted value') ;
end if;
END;
if you don't like to raise errors and just keep old values as I think so, use this code:
CREATE or REPLACE TRIGGER pro
BEFORE UPDATE OF price
ON products
FOR EACH ROW
declare
pr products.price%type;
BEGIN
if :new.price > 4000 then
:new.price := :old.price;
:new.product_ID := :old.product_ID;
end if;
END;
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;
/