Trigger adds a value with sequence CAN`T solve - sql

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;
/

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();

Trigger not working in PLSQL while retrieving new value

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;
/

Creating a trigger to update total count when any new employee is inserted

I want to create trigger in which update total count when any new employee inserted in pgadmin 4
here is my code
CREATE TRIGGER test_trigger
AFTER INSERT ON employee
FOR EACH ROW
UPDATE counter SET counter.count = counter.count + 1
You must put the update in the Trigger function
CREATE OR REPLACE FUNCTION update_counter()
RETURNS trigger
AS $$
BEGIN
UPDATE counter SET count = count + 1; --where clause?
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
The Trigger should call that function.
CREATE TRIGGER test_trigger
AFTER INSERT ON employee
FOR EACH ROW
EXECUTE procedure update_counter();
DEMO

Postgresql delete record involving two tables and store this record in the third record

I'm trying to delete a record from the table 'student', where on a Cascade delete it will remove it from the 'entry' table. But before delete i need to store this record in the third table 'cancel'.
Here is what i worked out so far:
DELETE FROM "CMPS".student
WHERE sno = '1';
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row
CREATE OR REPLACE FUNCTION trigger_backup_row(integer)
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (NEW.eno, NEW.excode, NEW.sno);
RETURN NEW;
END;
$$
language PLPGSQL
But comes back with an errors. Any help will be much appreciated.
I suppose you need:
CREATE OR REPLACE FUNCTION trigger_backup_row()
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (OLD.eno, OLD.excode, OLD.sno);
RETURN OLD;
END;
$$
language PLPGSQL
;
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row()
;
trigger function do not use arguments
on delete yo udon't have any NEW row - just an OLD one

Column values as factorial in Oracle SQL

I have created a trigger, that will autoincrement the id, according to the sequence, every time a new record is inserted. Like this:
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
--drop trigger test_trigger;
create or replace trigger test_trigger
before insert on myTable
for each row
begin
select test_seq.nextval into :new.tab_id from dual;
end;
However, I'd like to insert a factorial of the row index instead. How could I achieve this?
Edit:
create or replace trigger test_trigger
after insert on myT
for each row
begin
select fac(test_seq.nextval) into :new.tab_id from dual;
end;
Added fac function which works fine:
create or replace function fac(n in number)
return number
is
v number :=1;
begin
for i in 1..n
loop
v :=v * i;
end loop;
return v;
end;
But I still only see 1,2,3,4 in the table instead of 1,2,6,24...
From Oracle's documentation. You want to use a BEFORE trigger in this instance, an AFTER trigger won't actually change the table's data just from setting it in NEW:
Because the trigger uses the BEFORE keyword, it can access the new
values before they go into the table, and can change the values if
there is an easily-corrected error by assigning to :NEW.column_name.
My guess is that you are still seeing the same old values from the sequence because your BEFORE trigger still exists; the AFTER trigger won't change those values.
So what you want is the following:
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON myt
FOR EACH ROW
BEGIN
SELECT FAC(test_seq.nextval) INTO :new.tab_id FROM dual;
END;
/
I think as of Oracle 11g (or maybe it's 10g; can't remember) you can also do the following:
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON myt
FOR EACH ROW
BEGIN
:new.tab_id := FAC(test_seq.nextval);
END;
/
Do something like
create function factorial (n integer) return integer as
...
create or replace trigger test_trigger
after insert on mytable
-- don't do this for each row
begin
update mytable set
tab_id = factorial((select count(*) from mytable))
where tab_id is null;
end;
/