SQL Trigger Bad Bind Error - oracle

I'm trying to create a trigger that will fire if the Prog_Type = 'EPISODE'. I am receiving a bad binding error - PLS - 000049. I believe there is something wrong with my DECLARE state
CREATE OR REPLACE TRIGGER Seas_Pk_Trigger
BEFORE INSERT OR UPDATE OF Seas_ID ON Season_Table
FOR EACH ROW
DECLARE
Prog_Type VARCHAR2(7);
BEGIN
IF (:OLD.Prog_Type <> 'EPISODE')
THEN SELECT Seas_ID_Seq.nextval into :new.Seas_ID from dual;
END IF;
END Seas_Pk_Trigger;
/

If you are referencing a column in your table, you do not need to declare the column Prog_Type to reference it with a :OLD or :NEW pseudo-record. If Prog_Type is in your table (which I assume it is), then just omit the declaration:
CREATE OR REPLACE TRIGGER Seas_Pk_Trigger
BEFORE INSERT OR UPDATE OF Seas_ID ON Season_Table
FOR EACH ROW
BEGIN
IF (:OLD.Prog_Type <> 'EPISODE')
THEN SELECT Seas_ID_Seq.nextval into :new.Seas_ID from dual;
END IF;
END Seas_Pk_Trigger;
/
I'm not sure how to interpret your statement that you only want this to fire when Prog_Type = 'EPISODE' as your trigger has a <> to EPISODE clause?

Related

How to solve a bad bind variable for an oracle trigger>?

When I create the below tables and trigger, I'm getting a compiling bad bind error.
CREATE TABLE cteam_ExpenseItem (
ExpenseNo NUMBER
);
CREATE TABLE cteam_ExpenseReport (
ERSubmitNo NUMBER
);
CREATE OR REPLACE TRIGGER cteam_Trigger3
BEFORE INSERT OR UPDATE OF ExpenseNo ON cteam_ExpenseItem
FOR EACH ROW
DECLARE
vA cteam_ExpenseItem.ExpenseNo%TYPE;
BEGIN
SELECT ExpenseNo
INTO vA
FROM cteam_ExpenseItem
WHERE ExpenseNo = :NEW.ERSubmitNo;
IF vA <= ERSubmitNo THEN
RAISE_APPLICATION_ERROR(-20000, 'Error');
END IF;
END;
I'm getting a bad bind error for 'NEW.ERSUBMITNO'.
How do I go about solving this?
As #stickybit pointed out in a comment, there is no ERSubmitNo column on cteam_ExpenseItem. But even if there was, you don't want to try reading from the table your trigger is defined on - you're likely to get a TABLE IS MUTATING, TRIGGER CANNOT SEE IT error. Instead, use the value from the :OLD pseudo-row:
CREATE OR REPLACE TRIGGER cteam_Trigger3
BEFORE INSERT OR UPDATE OF ExpenseNo ON cteam_ExpenseItem
FOR EACH ROW
DECLARE
vA cteam_ExpenseItem.ExpenseNo%TYPE;
BEGIN
IF :OLD.ExpenseNo <= :NEW.ExpenseNo THEN
RAISE_APPLICATION_ERROR(-20000, 'Error');
END IF;
END;
I'm guessing that's what you're trying to do - if not you can fold, spindle, or mutilate as necessary. :-)

Oracle update trigger on the same table

I want to update field data_aktualizacji when some row in the same table is updated. I created the following compound trigger.
CREATE OR REPLACE TRIGGER oferta_update_trigger
FOR UPDATE ON oferty
compound TRIGGER
id_oferty number(10);
AFTER EACH ROW IS
BEGIN
id_oferty := :new.idk;
END AFTER EACH ROW;
AFTER STATEMENT IS
BEGIN
UPDATE oferty SET data_aktualizacji = SYSDATE WHERE idk = id_oferty;
END AFTER STATEMENT;
END;
/
When I want to update some record, I get the following error.
SQL Error: ORA-00036: maximum number of recursive SQL levels (50) exceeded.
How to solve this problem? I this that some loop is created, but I don't know, how to workaround this.
Update oracle to alter the column to default to sysdate
Alter table oferty alter column data_aktualizacji set default sysdate
No need for trigger at all
As Ctznkane525 wrote, you definitively should use default-value to perform this action.
If you don't want to use default you can modify new.data_aktualizacji:
CREATE OR REPLACE TRIGGER oferty_update_aktualizacji
BEFORE INSERT OR UPDATE
ON oferty
FOR EACH ROW
DECLARE
BEGIN
:new.data_aktualizacji:= sysdate;
END;

How to declare one variable for each row in trigger?

I have following trigger:
create or replace TRIGGER MY_TIGGER_NAME AFTER UPDATE ON MY_TABLE
REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
WHEN ( NEW.STATUS = ANY (10,40,42,44,46,50,60) and OLD.STATUS != NEW.STATUS)
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF :NEW.ALERT is NULL
THEN
dbms_alert.signal('print_update_event','update_message');
ELSE
dbms_alert.signal( :NEW.ALERT,'update_message');
END IF;
commit;
END;
I would like to change it because it send alert for each row. I would like to send only one alert if more than one row with ALERT column equal NULL was updated and I would like to send one alert for each row with ALERT column NOT equal NULL.
As I understand Oracle variables I can declare local variable in my trigger but this variable will be declared separately for each row so following change has no sense:
create or replace TRIGGER MY_TIGGER_NAME AFTER UPDATE ON MY_TABLE
REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
WHEN ( NEW.STATUS = ANY (10,40,42,44,46,50,60) and OLD.STATUS != NEW.STATUS)
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
flag NUMBER(1,0) :=0;
BEGIN
IF :NEW.ALERT is NULL and flag=0
THEN
dbms_alert.signal('print_update_event','update_message');
flag:=1;
ELSE
dbms_alert.signal( :NEW.ALERT,'update_message');
END IF;
commit;
END;
Package variable could be used instead local variable but I think that package variable is bad idea because two triggers can be executed in parallel. Maybe I am wrong.
I attach diagram of trigger which shows what I would like to achieve.
How to do it?

Oracle auto increment trigger issue

I am having an issue when creating the auto increment trigger in Oracle 11g. If someone can point out what I am doing wrong, I would really appreciate it. My script for the sequence is this :
CREATE SEQUENCE SPSS_QUOTE_LINE_ITEMS_SEQ start with 1
increment by 1
minvalue 1;
The script for trigger:
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
BEGIN
SELECT SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL
INTO :new.line_num
FROM dual;
END;
The error I am getting:
[Code: 900, SQL State: 42000] ORA-00900: invalid SQL statement
Thanks a lot.
The correct way of doing this to modify your trigger as below:
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
BEGIN
:new.line_num := SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL;
--No need to write any Select Into statement here.
END;
Or if i follow your way then it goes like
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
declare
var number;
BEGIN
SELECT SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL
INTO var
FROM dual;
:new.line_num :=var;
END;
You normally use the terms in a trigger using :old to reference the old value of the column and :new to reference the new value.

Trigger using 2 tables

Is it possible to reference a second table within a trigger?
create or replace trigger table1
before update of status_code on table1
for each row
declare z_user_id table2.user_id;
begin
if :new.status_code in (30,40) then
:new.z_open_01 := nvl(:OLD.z_user_id, nvl(:NEW.z_user_id, :old.z_open_01));
end if;
end;
/
As to your question: Yes.
The :old and :new constructs only apply to the table that cause the trigger to fire.
The way you have that written you are triggering off of Table1, but your description sounds like you want to grab a value from table one when something in Table2 changes.
But you didn't say what you want to DO with the userid you grab.
If you create a trigger on Table2, read a value from Table1, and save the value in a column in the row that fired the trigger you are gtg. Other scenarios get more complex.
so, based on comments something like:
create or replace trigger table2
after update of status_code on table2
for each row when (:new.status_code in (30,40))
declare t1_user Table1.user_id%type;
begin
Select user_id into t1_user from Table1 where <condition>
:new.z_open_01 := t1_user;
end if;
end;
/
setting up whatever condition select the desired user from Table1.

Resources