Cx_oracle trigger error - oracle

I am trying to create a simple trigger in cx_oracle which copies values in a table called Student when some value is inserted in the table studentTemp. There are only two columns in each table namely stud_ID and stud_Name. When I try to insert value in StudentTemp by using
cur.execute("INSERT INTO studentTemp VALUES(1, 'Bob')")
I am getting error DatabaseError: ORA-04098: trigger 'S12345.INSERT_STUD' is invalid and failed re-validation. Following is the code
cur.execute('''CREATE OR REPLACE TRIGGER insert_Stud
AFTER INSERT ON studentTemp
FOR EACH ROW
BEGIN
INSERT INTO Student(Stud_ID,Stud_Name) VALUES
(:new.Stud_ID, :new.Stud_Name);
END;''')
I have also tried
cur.execute('''CREATE OR REPLACE TRIGGER insert_Stud
AFTER INSERT ON studentTemp
REFERENCING NEW AS new
FOR EACH ROW
BEGIN
INSERT INTO Student(Stud_ID,Stud_Name) VALUES (:new.Stud_ID, :new.Stud_Name);
END;/''')
But still get the same error
I can get it work if I use a stored procedure like this
# create insertStudent() stored procedure
cur.execute('''CREATE OR REPLACE PROCEDURE insertStudent(
sID IN STUDENT.STUDENT_ID%TYPE,
sName IN STUDENT.STUDENT_NAME%TYPE)
IS
BEGIN
INSERT INTO STUDENT VALUES(sID, sName);
END;''')
# create insert_Stud() trigger
cur.execute('''CREATE OR REPLACE TRIGGER insert_Stud
AFTER INSERT ON studentTemp
FOR EACH ROW
BEGIN
insertStudent(:new.Stud_ID, :new.Stud_Name);
END;''')
Can someone kindly tell me how to fix this. Thanks

It seems to me like you have an error in naming your Student-Table Fields.
In your Procedure, the colums are referenced as
STUDENT.STUDENT_ID
STUDENT.STUDENT_NAME
In your trigger, you call them
INSERT INTO Student(**Stud_ID**,**Stud_Name**) VALUES ...

Try using SQL*Plus to create the trigger. Then use the "show errors" command to see what the problem is. After that you should be able to use cx_Oracle without any difficulty. The only difference is that the trailing / in SQL*Plus does not need to be there.

Related

Create Oracle Trigger to execute PL/SQL program when row is inserted to table

I want to create a trigger in Oracle 11g, that is fired when a row is inserted to the table DLGPROD.INV_CHEMICAL when the value for the BATCHTYPE column is equal to 'STANDARD'.
The trigger will execute a PL/SQL program called SYS.STDEMAIL
When I try to call the STDEMAIL program, I get PLS-00331 illegal reference.
I am pretty new to working with triggers or PL/SQL, and I can't figure out how to tell the trigger #1 - to only fire when a row is updated AND the BATCHTYPE column value is 'STANDARD', and #2 - how to correctly call the PL/SQL program called STDEMAIL belonging to the SYS schema.
Any assistance would be greatly appreciated.
From the docs (http://docs.oracle.com/cd/B10501_01/appdev.920/a96590/adg13trg.htm)
#1 Here you can see that Emp_tab is the table you are watching, and Sal is a column. Using the new keyword as in new.Sal you can check the new inserted value.
CREATE OR REPLACE TRIGGER Log_salary_increase
AFTER UPDATE ON Emp_tab
FOR EACH ROW
WHEN (new.Sal > 1000)
BEGIN
INSERT INTO Emp_log (Emp_id, Log_date, New_salary, Action)
VALUES (:new.Empno, SYSDATE, :new.SAL, 'NEW SAL');
END;
#2 You can use the call keyword to execute an existing procedure
CREATE OR REPLACE TRIGGER logontrig AFTER LOGON ON DATABASE
-- Just call an existing procedure. The ORA_LOGIN_USER is a function
-- that returns information about the event that fired the trigger.
CALL foo (ora_login_user)
/

Oracle Trigger inserting row into custom table

I have a Post-Insert Oracle trigger that is simply going to be used to insert a row into a table when the triggered table has a record added to it. My code is a simple insert:
BEGIN
INSERT INTO R5CHECKLISTMAP(CLM_CHKLINECODE, CLM_SYSTEMCODE)
VALUES(10, 'TEST');
END;
However, I get the error:
ORA 01006 - Bind Variable does not exist POST-INSERT 10 Before Binding
I know this has to be something simple, but I cannot figure out what the problem could be.

How to avoid Getting ORACLE Mutating trigger error

I created the trigger to update the oracle data base table after insert.
CREATE OR REPLACE TRIGGER Update_ACU
AFTER INSERT ON TBL_ACU
FOR EACH ROW
BEGIN
UPDATE TBL_ACU
SET CURRENCY = 'XXX'
WHERE ACCOUNT like '%1568';
END ;
I inserted record as
insert into TBL_ACU values('23','USD','1231568');
I am getting table ORACLE Mutating trigger error.
Please help me how to resolve this.
It would be better to use BEFORE INSERT trigger to do this.
Try like this,
CREATE OR REPLACE
TRIGGER update_acu
BEFORE INSERT ON tbl_acu
FOR EACH ROW
WHEN (NEW.ACCOUNT LIKE '%1568')
BEGIN
:NEW.currency := 'XXX';
END ;
Well, you cannot modify the table from the trigger if the trigger is called upon modification of that table. There are various solutions to this problem including an AFTER STATEMENT trigger and caching modifications in some collection defined in PL/SQL PACKAGE, howewer in your situation I'd rather change the body of your trigger to this:
BEGIN
IF :NEW.ACCOUNT LIKE '%1568' THEN
:NEW.CURRENCY := 'XXX';
END IF;
END;
You can use the :NEW and :OLD variables inside the trigger, which identify the new and old values of the record accordingly. Modifying values of the :NEW record will cause changes in data actually inserted to the database.

create trigger for copying values between tables in oracle

I am new to the sql. I want to create a trigger for copying values between tables.
basically, the task I want to finish is forwarding students' message table values to specific staff_maibox
here is the code.
drop trigger forward_msg_to_staff;
create or replace trigger forward_msg_to_staff
update on message
for each row
declare
message_id VARCHAR2(10);
client_id NUMBER(10);
staff_id NUMBER(5);
message_date DATE;
message_title VARCHAR2(20);
staff_mailbox VARCHAR2(255);
begin
insert into staff_mailbox(message_id, client_id, staff_id, message_date, message_title, staff_mailbox)
values(:new.message_id, :new.client_id, :new.staff_id, :sysdate, :new.message_title, :old.staff_mailbox)
end;
/
is this code correct?
Please advise. thanks in advance.
You're getting an error because you're missing either the BEFORE or AFTER keyword from the CREATE TRIGGER statement.
These are required as indicated in the documentation:
Additionally:
There's no need to declare all the variables, you're not using them
:sysdate is incorrect, you're not binding it. You can just use sysdate instead as you would in standard SQL or PL/SQL.
You're missing a semi-colon after the VALUES clause of the INSERT statement.
Putting this together your trigger may look like this
create or replace trigger forward_msg_to_staff
after update on message
for each row
begin
insert into staff_mailbox( message_id, client_id, staff_id, message_date
, message_title, staff_mailbox )
values ( :new.message_id, :new.client_id, :new.staff_id, sysdate
, :new.message_title, :old.staff_mailbox );
end forward_msg_to_staff;
/
Note that I've used the trigger name in the END as well. This is for convenience only, it makes it obvious where the trigger ends...
If you want to see what errors your're getting when you're creating a trigger use show errors as a_horse_with_no_name suggests. This shows any compilation errors, which is invaluable for tracking them down.

Store the data of deleted record using trigger

I want to write a trigger which fires on deletion of a record from a table, and inserts a record in another table and uses the details of the record deleted.
Database : Oracle 10g
My trigger looked like this
CREATE or REPLACE TRIGGER myTrigger
AFTER DELETE
ON myTable
REFERENCING NEW AS old_tab
FOR EACH ROW
BEGIN
INSERT INTO ACTIVITYLOG values ('ADMIN',:old_tab.tabletID,'MIGRATION','ERROR','TEST','T','NIL',sysdate)
END;
here :old_tab.tabletID the tabletID is the column of the table myTable in which deletion is done.
I want to save the I and a log that it was deleted.
But when I try deleting a record I get the following error
Error code 4098, SQL state 42000: ORA-04098: trigger 'DB.MYTRIGGER' is
invalid and failed re-validation
P.S. Ran the trigger creation in NetBeans SQL Editor.
Here is the,
EDIT
STRUCTURE OF myTable (Table deletion occurs)
tabletID varchar2(15) PRIMARY KEY
tabletName varchar2(100)
STRUCTURE OF ACTIVITYLOG
username varchar2(15)
tabletKey varchar2(15)
page_ref varchar2(100)
errors varchar2(100)
remarks varchar2(100)
operationcode char(2)
lastupdateip varchar2(20)
lastupdatedate date
Sorry don't have access to SQL PLUS EDITOR.
You should use the :OLD values rather than the :NEW values. The :NEW values in a DELETE trigger (whether BEFORE or AFTER) are blank. This makes sense, because if you think about it the record has logically ceased to exist at this point.
However that is not a source of compilation errors.
"still the same error shows up on deletion. "
I suppose we could spend all day guessing what's wrong so let's stop now. You can discover the compilation errors with this simple query:
select * from user_errors
where name = 'MYTRIGGER'
and type = 'TRIGGER'
"I changed the :NEW to :OLD, and added a semicolan and ran it on SQL
PLUS, and that did the trick"
For the benefit of future here is a version of the trigger which will compile and which will correctly write the required values:
CREATE or REPLACE TRIGGER myTrigger
AFTER DELETE
ON myTable
REFERENCING OLD AS old_tab
FOR EACH ROW
BEGIN
INSERT INTO ACTIVITYLOG values ('ADMIN',:old_tab.tabletID,'MIGRATION','ERROR','TEST','T','NIL',sysdate);
END;
/
The problem is this:
REFERENCING NEW AS old_tab
You've redefined the NEW values with the label "old_tab". This is somewhat like adding #define FALSE TRUE to the top of a program.
Add a semicolon after the insert statement
Because you're using an AFTER DELETE trigger, you only need to access the :OLD values, e.g.:
CREATE or REPLACE TRIGGER myTrigger
AFTER DELETE
ON myTable
FOR EACH ROW
BEGIN
INSERT INTO ACTIVITYLOG values ('ADMIN',:OLD.tabletID,'MIGRATION','ERROR','TEST','T','NIL',sysdate);
END;

Resources