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

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
/

Related

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;
/
....

Create trigger in oracle doesn't find sequence

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 ?

Migration from Oracle to MariaDB - Trigger, Self-referencing table

I'm migrating my Oracle database to MariaDB, and I am unsure how to create a self-referencing ID on one of my tables.
Essentially, if a belongs_to_id is not specified, it will assume that it belongs to itself (and be the same as the ID generated ON INSERT); however, if a belongs_to_id has been specified, then it will use that instead.
The reason I do this is because I have posts and replies stored in the same table. If the id = belongs_to_id then it's an initiating post, whereas if it differs, it is a reply to another post.
I've thought about letting my application (Node JS) do a quick UPDATE using last_insert_id - but it seems rather messy and I'd like to avoid that if possible.
Here's what I use in Oracle - does anyone know how I replicate this in MariaDB please?
CREATE OR REPLACE TRIGGER my_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
:new.id := my_sequence.NEXTVAL;
:new.belongs_to_id := NVL(:new.belongs_to_id,:new.id);
END;
/
This should work in MariaDb as it closely conforms to SQL-99 syntax.
CREATE OR REPLACE TRIGGER my_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
:new.id := my_sequence.NEXTVAL;
:new.belongs_to_id := NVL(:new.belongs_to_id,:new.id);
END;
IF you set your new ID to be an auto increment then you can only use an after insert trigger. Something like
CREATE OR REPLACE TRIGGER my_trigger
AFTER INSERT ON my_table
FOR EACH ROW
v_id NUMBER:
BEGIN
v_id := LAST_INSERT_ID();
:old.belongs_to_id := NVL(:old.belongs_to_id,v_id);
END;
Maybe simply
When INSERTing, set belongs_to_id to NULL (no TRIGGER).
When SELECTing, do COALESCE(belongs_to_id, id).
Meanwhile, completely switch from SEQUENCE to AUTO_INCREMENT (again no TRIGGER).

Oracle procedure inside a trgigger to check the output

I am trying to call my procedure inside a trigger.
The trigger is created successfully but the procedure is calling being called.
I am using oracle 10g with SQL developer tool.
I have tried to see the dbms_output but nothing shows in DBMS_OUTPUT console except"set serveroutput on" when i update the table.
My trigger is
create or replace
TRIGGER trig_sample
AFTER UPDATE
ON sample_table
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
SAMPLE_PROC(:NEW.PARAM1, :NEW.PARAM2 ,:NEW.PRAMA3);
END;
procedure is
CREATE or REPLACE Procedure SAMPLE_PROC(PARAM1 NUMBER,PARAM2 NUMBER,PARAM3 varchar2)
BEGIN
DBMS_OUTPUT.put_line('test : '||test);
UPDATE SAMPLE_TABLE SET STATUS = 'VA'
WHERE SAMPLE_ID = '2012';
END;
In my procedure i have used DBMS_OUTPUT.put_line('test : '||test);
but could not see any output. Please provide any suggestions to view the DBMS output in console and how to check the trigger is called or not?

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;
/

Resources