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;
/
Related
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.
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;
/
....
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 ?
I recently moved my database to a new server (both Oracle 11g).
In the new host, there is a weird behavior, that whenever I run something like:
create or replace test_trigger
before insert or update
on test_table
for each row
begin
select 1 from dual;
end;
/
The trigger created is invalid without any error in log. I tried to recompile, it still invalid.
It only happen with the function "create or replace". If I drop the trigger and re-create again, it would be valid.
My question is, did I config something incorrectly? How can I check it? Thank you.
Code you posted won't compile, not in any Oracle database I know. Why? Wrong syntax.
Here's a demonstration:
SQL> create or replace test_trigger
2 before insert or update
3 on test_table
4 for each row
5 begin
6 select 1 from dual;
create or replace test_trigger
*
ERROR at line 1:
ORA-00922: missing or invalid option
SQL> end;
SP2-0042: unknown command "end" - rest of line ignored.
SQL> /
So, what's wrong with it?
create (or replace) wants to know what you're going to create. "test_trigger"? As far as Oracle is concerned, that could be "mickey_mouse" and the result will be the same. It is the trigger keyword that is missing
SELECT in PL/SQL requires an INTO clause, so that you could store the result into something
in order to be able to do that, you have to declare a variable
Here's code that, actually, compiles:
SQL> create or replace trigger test_trigger --> this
2 before insert or update
3 on test_table
4 for each row
5 declare
6 l_dummy number; --> this
7 begin
8 select 1
9 into l_dummy --> this
10 from dual;
11 end;
12 /
Trigger created.
SQL>
So, it seems that you misinterpret reality.
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
/