Create trigger in oracle doesn't find sequence - oracle

I am trying to create a sequence and then use it in a table for auto increment.
I am using Oracle 10g.
CREATE SEQUENCE ALL_BANK_STMT_SEQ START WITH 1;
CREATE TRIGGER ALL_BANK_STMT_TRG
BEFORE INSERT ON ALL_BANK_STMT
FOR EACH ROW
BEGIN
SELECT ALL_BANK_STMT_SEQ.NEXTVAL INTO :NEW.BANK_STMT_LN_NO FROM SYS.DUAL;
END;
This gives an error ORA-02289 : sequence doesn't exist.
However , when I select from the sequence it work's fine but I can't seem to use it in a trigger i.e. I get results when I do :
select ALL_BANK_STMT_SEQ.NEXTVAL from dual;
Am I missing any permission to use it in the trigger ?

Related

Error while creating trigger on table in Oracle

I am creating a trigger for inserting incremental ID's in my table. But while creating I am getting below error
An error occurred while processing a recursive SQL statement
(a statement applying to internal dictionary tables).
ORA-00604: error occurred at recursive SQL level 1
ORA-01654: unable to extend index SYS.I_TRIGGERCOL1 by 64 in tablespace SYSTEM
Here is my trigger query.
create or replace TRIGGER TGR_IPCOLO_BIL
BEFORE INSERT ON ipcolo_ipfee_calc_bil
for each row
begin
IF INSERTING THEN
IF :NEW."ID" IS NULL THEN
select SEQ_IPCOLO_IPFEE_BIL.nextval into :NEW."ID" from dual;
end if;
END IF;
end;
That error sounds pretty bad (I never saw it before) ... internal dictionary tables?! What is error code? ORA-xxxxx?
Meanwhile, trigger can be simplified to this:
create or replace trigger trg_ipcolo_bil
before insert on ipcolo_ipfee_calc_bil
for each row
begin
:new.id := nvl(:new.id, seq_ipolo_ipfee_bil.nextval);
end;
/
You don't have to check if inserting; what else could it be, if it fires before insert? Also, you don't need select ... into - use sequence directly. nvl makes sure you won't overwrite id if you provided it.
Also, consider using identity column instead, if your database version supports it.

Oracle SQL - Running scripts

I'm trying to create an Oracle SQL script that defines the field and triggers for multiple tables as part of my initialization for the implementation of a new project. A sample of what I'm trying to do is shown here;
ALTER TABLE TBL_SAP_VENDORS
DROP COLUMN UPT_TS;
ALTER TABLE TBL_SAP_VENDORS
ADD (
UPT_TS TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- The automatic update of fields has to be performed using a trigger
-- First we drop the trigger incase it already exists
drop trigger vendors_updt_mark;
-- Then add the trigger deffinition
CREATE TRIGGER vendors_updt_mark
before insert or update
on TBL_SAP_VENDORS
FOR EACH ROW
BEGIN
:new.UPT_TS := SYSTIMESTAMP;
END;
-- INVENTORY
ALTER TABLE TBL_SAP_INVENTORY
DROP COLUMN UPT_TS;
ALTER TABLE TBL_SAP_INVENTORY
ADD (
UPT_TS TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- The automatic update of fields has to be performed using a trigger
-- First we drop the trigger incase it already exists
drop trigger inventory_updt_mark;
-- Then add the trigger deffinition
CREATE TRIGGER inventory_updt_mark
before insert or update
on TBL_SAP_INVENTORY
FOR EACH ROW
BEGIN
:new.UPT_TS := SYSTIMESTAMP;
END;
When I try executing this in the Oracle SQL Developer I get the message :
Table TBL_SAP_VENDORS altered.
Table TBL_SAP_VENDORS altered.
Trigger VENDORS_UPDT_MARK dropped.
Trigger VENDORS_UPDT_MARK compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
6/1 PLS-00103: Encountered the symbol "ALTER"
Errors: check compiler log
When I open up the compile log I see:
Error(5,1): PLS-00103: Encountered the symbol "ALTER"
How do I run these commands, of which I have about 20 to run in a single script?
thanks
You have to end the creation of a trigger (or any PL/SQL object, for that matter), with a slash on its own line.
...
CREATE TRIGGER vendors_updt_mark
before insert or update
on TBL_SAP_VENDORS
FOR EACH ROW
BEGIN
:new.UPT_TS := SYSTIMESTAMP;
END;
/
....

Cx_oracle trigger error

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.

creating insert trigger error

i'm trying to create a trigger that sets the id from a predefined sequence.
CREATE SEQUENCE seq_list_status
START WITH 1
INCREMENT BY 1
;
CREATE OR REPLACE TRIGGER trg_list_status_insert
BEFORE INSERT ON list_status
FOR EACH ROW
select seq_list_status.nextval into :new.id from dual;
/
i'm getting the error below while creating the trigger
Error starting at line 1 in command:
CREATE OR REPLACE TRIGGER trg_list_status_insert
BEFORE INSERT ON list_status
FOR EACH ROW
select seq_list_status.nextval into :new.id from dual
Error at Command Line:4 Column:4
Error report:
SQL Error: ORA-04079: invalid trigger specification
04079. 00000 - "invalid trigger specification"
*Cause: The create TRIGGER statement is invalid.
*Action: Check the statement for correct syntax.
i googled it but it seems all ok. any idea for what could be wrong?
Triggers are program units. Consequently we must wrap the code body in BEGIN and END. Try this
CREATE OR REPLACE TRIGGER trg_list_status_insert
BEFORE INSERT ON list_status
FOR EACH ROW
BEGIN
select seq_list_status.nextval into :new.id from dual;
END;
/
Unfortunately the examples in the SQL Reference are not as helpful as we would like. But it does link to other useful documents, such as the App Developers Guide.
you are missing the begin end
CREATE OR REPLACE TRIGGER trg_list_status_insert
BEFORE INSERT ON list_status
FOR EACH ROW
BEGIN
select seq_list_status.nextval into :new.id from dual;
END;
/

Why would a database trigger be invalid in an oracle DB?

I've created a trigger manually (By pasting the SQL from another oracle db into the management console) which auto increments the primary key of a table and when I look at the trigger in the object browser The trigger is listed as invalid.
Why would this be the case?
Here is the SQL that recreates the trigger:
CREATE OR REPLACE TRIGGER "BI_AGENTEVENTDATA"
before insert on "AGENTEVENTDATA"
for each row
begin
select "AGENTEVENTDATA_SEQ".nextval into :NEW.ID from dual;
end; ;
/
ALTER TRIGGER "BI_AGENTEVENTDATA" ENABLE
/
are you sure that the sequence exists and can be used by the trigger?
Try this:
select sequence_name from user_sequences;
Try the trigger without the " . In other words:
CREATE OR REPLACE TRIGGER "BI_AGENTEVENTDATA"
before insert on "AGENTEVENTDATA"
for each row
begin
select AGENTEVENTDATA_SEQ.nextval into :NEW.ID from dual;
end; ;
/
ALTER TRIGGER "BI_AGENTEVENTDATA" ENABLE
/

Resources