How can I work around the Oracle's limitation of not allowing subqueries in triggers.
Here's an example trigger I'm trying to create, but am unable to because I can't use a subquery.
CREATE OR REPLACE TRIGGER trigger_w_subquery
AFTER UPDATE OR INSERT ON project_archiving
FOR EACH ROW WHEN (old.archiving_status <> new.archiving_status
AND new.archiving_status = 1
AND (SELECT offer FROM projects WHERE projnum = :new.projnum) IS NULL
)
BEGIN
INSERT INTO offer_log (offer, status, date)
VALUES (null, 9, sysdate);
END;
This trigger would do it:
CREATE OR REPLACE TRIGGER trigger_w_subquery
AFTER UPDATE OR INSERT ON project_archiving
FOR EACH ROW WHEN (old.archiving_status <> new.archiving_status
AND new.archiving_status = 1
)
DECLARE
l_offer projects.offer%TYPE;
BEGIN
SELECT offer INTO l_offer
FROM projects
WHERE projnum = :new.projnum;
IF l_offer IS NULL THEN
INSERT INTO offer_log (offer, status, date)
VALUES (null, 9, sysdate);
END IF;
END;
I have assumed that the select from projects will always find a row; if not it will raise a NO_DATA_FOUND exception that you may need to handle.
I expect that you want something like
CREATE OR REPLACE TRIGGER trigger_w_subquery
AFTER UPDATE OR INSERT ON project_archiving
FOR EACH ROW
WHEN (old.archiving_status <> new.archiving_status
AND new.archiving_status = 1)
DECLARE
l_offer projects.offer%TYPE;
BEGIN
SELECT offer
INTO l_offer
FROM projects
WHERE projnum = :new.projnum;
IF( l_offer IS NULL )
THEN
INSERT INTO offer_log (offer, status, date)
VALUES (null, 9, sysdate);
END IF;
END;
Can you put the condition into the action (between BEGIN and END) instead of in the 'whether it fires'? Yes, it means that the trigger body might be fired more often - but if it gets you around the problem...
Related
CREATE OR REPLACE TRIGGER TR_BCK_ANAG
BEFORE DELETE OR INSERT OR UPDATE ON ANAGRAFICA
FOR EACH ROW
BEGIN
SELECT ID ,
NOME ,
COGNOME ,
DATA ,
NUMERO_DI_TELEFONO ,
COMUNE
INTO BCK_ANAG
FROM ANAGRAFICA;
END;
Why does oracle give me back error
PLS-00403:expression 'BCK_ANAG' cannot be used as an INTO-target of a
SELECT/FETCH statement
and how can i solve this?
CREATE OR REPLACE TRIGGER TR_BCK_ANAG
BEFORE DELETE OR INSERT OR UPDATE
ON ANAGRAFICA
FOR EACH ROW
BEGIN
INSERT INTO BCK_ANAG (ID,
NOME,
COGNOME,
DATA,
NUMERO_DI_TELEFONO,
COMUNE)
SELECT :NEW.ID,
:NEW.NOME,
:NEW.COGNOME,
:NEW.DATA,
:NEW.NUMERO_DI_TELEFONO,
:NEW.COMUNE
FROM DUAL;
END;
I would assume this is what you're trying to do.
I am trying to write cursor that would insert into table, but I am receiving error, need help with this. Error that I am receiving is ORA-06550.
DECLARE
CURSOR cur_rating IS
SELECT bc.name, bc.title, bc.checkoutdate, bc.returneddate,
b.categoryname,b.publisher, ba.authorname
FROM bookshelf_checkout bc INNER JOIN bookshelf b
ON bc.title = b.title
INNER JOIN bookshelf_author ba
ON bc.title = ba.title
FOR UPDATE NOWAIT;
lv_totdays_num NUMBER(4) := 0;
lv_rating_txt VARCHAR2(2);
BEGIN
FOR rec_rating IN cur_rating LOOP
lv_totdays_num := rec_rating.returneddate -
rec_rating.checkoutdate;
IF lv_totdays_num <= 10 THEN lv_rating_txt := 'DR';
ELSIF lv_totdays_num <= 25 THEN lv_rating_txt := 'CR';
ELSIF lv_totdays_num <= 35 THEN lv_rating_txt := 'BR';
ELSE lv_rating_txt := 'A';
END IF;
INSERT INTO bookshelf_audit (title, publisher, categoryname,
new_rating, auditdate)
VALUES (rec_rating.title, rec_rating.publisher,
rec_rating.categoryname, lv_rating_txt, sysdate)
WHERE CURRENT OF cur_rating;
END LOOP;
COMMIT;
END;
You need to remove the where clause from your insert ... values statement:
INSERT INTO bookshelf_audit
(title, publisher, categoryname,
new_rating, auditdate)
VALUES
(rec_rating.title, rec_rating.publisher,
rec_rating.categoryname, lv_rating_txt, sysdate)
WHERE CURRENT OF cur_rating;
should be
INSERT INTO bookshelf_audit
(title, publisher, categoryname,
new_rating, auditdate)
VALUES
(rec_rating.title, rec_rating.publisher,
rec_rating.categoryname, lv_rating_txt, sysdate);
The WHERE CURRENT OF clause in an UPDATE or DELETE statement states that the most recent row fetched from the table should be updated or deleted :
UPDATE table_name
SET set_clause
WHERE CURRENT OF cursor_name;
OR
DELETE FROM table_name
WHERE CURRENT OF cursor_name;
but not applicable for an INSERT statement.
So, remove WHERE CURRENT OF cur_rating part only, your code will run. That's make your INSERT statement as :
INSERT INTO bookshelf_audit (title, publisher, categoryname,
new_rating, auditdate)
VALUES (rec_rating.title, rec_rating.publisher,
rec_rating.categoryname, lv_rating_txt, sysdate);
Is there equivalent to this T-SQL query in PL/SQL (Oracle 12c)?
UPDATE A SET A.columnA = 10 WHERE A.columnB < 30 OUTPUT INSERTED.*, DELETED.*
The query updates table A and at the same time returns the status of the record before the update and after the update.
Trigger is not a solution for me as well as SELECT records before and SELECT records after updating.
Not a direct one, but using RETURNING INTO you will be able to achieve the same effect:
CREATE TABLE A(columnA VARCHAR2(10), columnB INT);
INSERT INTO A(columnA, columnB) VALUES ('Test', 10);
INSERT INTO A(columnA, columnB) VALUES ('Row 2', 20);
CREATE TABLE audit_table(col_new VARCHAR2(10),col_old VARCHAR2(10));
DECLARE
TYPE rec IS RECORD (actual A.columnA%TYPE, old A.columnA%TYPE);
TYPE col_a_t IS TABLE OF rec;
v_a col_a_t;
BEGIN
UPDATE (SELECT A.*, (SELECT A.columnA FROM dual) AS old_columnA FROM A)
SET columnA = 'XYZ'
WHERE columnB < 30
RETURNING columnA, old_columnA BULK COLLECT INTO v_a;
COMMIT;
-- printing for debug
FOR i IN v_a.first .. v_a.last LOOP
dbms_output.put_line('Old =>' || v_a(i).old || ' new => ' || v_a(i).actual);
END LOOP;
-- additional
FORALL i IN v_a.first .. v_a.last
INSERT INTO audit_table VALUES v_a(i);
COMMIT;
END;
/
SELECT * FROM A;
SELECT * FROM audit_table;
DBFiddle Demo
Idea taken from: Returning Old value during update
I have created a trigger, that will automatically set the first column values as subsequent factorial numbers. However, additionally, I would like to set the second column's value as the value of first incremented by 5, in case a null value is inserted. Here's what I try right now:
create or replace trigger test_tr
before insert on myT
for each row
begin
IF :new.mNumb is null
THEN
UPDATE myT
SET mNumb = :new.tab_id + 5;
END IF;
SELECT fac(test_seq.NEXTVAL)
INTO :new.tab_id
FROM dual;
end;
But clearly I'm missing something, as nothing happens, the inserted null is still empty.
Do not re-update the table in your trigger, update the row you're given directly:
...
IF :new.mNumb is null
THEN
:new.mNumb = :new.tab_id + 5;
END IF;
...
It all works as expected, using Emmanuel's suggestion to remove the update stmt, as far as I can tell. Here's the test case I used:
drop table test;
create table test (col1 number, col2 number);
create trigger test_trg
before insert on test
for each row
begin
IF :new.col2 is null
THEN
:new.col2 := :new.col1 + 5;
END IF;
:new.col1 := dbms_random.value;
end;
/
insert into test values (1, 1);
insert into test values (1, null);
insert into test values (null, null);
commit;
select * from test;
which produces the following output:
COL1 COL2
---------- ----------
.617580128 1
.030570358 6
.555066268
Maybe if you set :new.col1 before dealing with the null col2 scenario, that would work better for you? Doing that produces:
COL1 COL2
---------- ----------
.302670917 1
.024927489 5.02492749
.667568400 5.66756840
I have write a PL/SQL insert trigger but it doesn't compile properly but I don't see the problem.
create or replace TRIGGER trg_NEWROW
BEFORE INSERT or UPDATE OF STATUS_ID ON application FOR EACH ROW
BEGIN
INSERT INTO APPLICATION_HISTORY
(APPLICATION_ID, STATUS_ID, DATE_OF_CHANGE)
VALUES
(1, 1, SYSDATE)
END;
Try this: You missed a ';' at the end of Insert statement
CREATE OR REPLACE TRIGGER TRG_NEWROW
BEFORE INSERT OR UPDATE OF STATUS_ID
ON APPLICATION
FOR EACH ROW
BEGIN
INSERT INTO
APPLICATION_HISTORY ( APPLICATION_ID,
STATUS_ID,
DATE_OF_CHANGE )
VALUES
( 1,
1,
SYSDATE );
END;
/