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.
Related
I seem to get ORA-00984 error when inserting the inputs to the sql statment
PLSQL code:
CREATE OR REPLACE PROCEDURE do_insättning (pnr_in in insättning.pnr%type,
knr_in in insättning.knr%type,
belopp_in in insättning.belopp%type,
datum_in in insättning.datum%type)
IS
BEGIN
INSERT INTO insättning
VALUES (radnr_seq,pnr_in,knr_in,belopp_in,datum_in);
END;
The design of the table:
Also the radnr_seq:
CREATE SEQUENCE radnr_seq START WITH 1 INCREMENT BY 1;
What I have tried is to check is that the sql statemtn only inserts to one column like this:
INSERT INTO insättning (radnr) values (radnr_seq);
Aswell as checking for invalid values but to no result.
You need to insert the sequence number nextval radnr_seq.nextval.
You need to call nextval for sequence :
CREATE OR REPLACE PROCEDURE do_insättning (pnr_in in insättning.pnr%type,
knr_in in insättning.knr%type,
belopp_in in insättning.belopp%type,
datum_in in insättning.datum%type)
IS
BEGIN
INSERT INTO insättning
VALUES (radnr_seq.NEXTVAL,pnr_in,knr_in,belopp_in,datum_in);
END;
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;
I have problem when trying to excute a sequence with a before insert trigger.
CREATE TABLE personne (ID number, nom varchar2(250 char));
CREATE SEQUENCE s_inc_pers START WITH 1 INCREMENT BY 1;
CREATE TRIGGER tr_inc_pers ON t1 BEFORE INSERT
FOR EACH ROW
DECLARE
BEGIN
select s_inc_pers into :new.t1.ID from DUAL;
END.
Oracle reports a bad bind variable if you try to reference something with :NEW that is not a column in the target table. In this case it may be prompting you for a bind value as the statement is malformed.
The NEW pseudorecord refers to the row in the triggering table that is being affected by the statement. You don't need to (and mustn't) include the table name when you use the pseudorecord field, so :new.t1.ID should just be :new.ID.
To get the next value from the sequence you need to use nextval, you can't only provide the sequence name.
Your clauses are also in the wrong order, you need the DML event (insert) befroe the target table.
CREATE TRIGGER tr_inc_pers BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
select s_inc_pers.nextval into :new.ID from DUAL;
END;
As you are using 11g you don't even need to select from dual, you can just assign the column value:
CREATE TRIGGER tr_inc_pers BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
:new.ID := s_inc_pers.nextval;
END;
I am a novice in PL/SQL and DB objects. I have written an after insert trigger based on condition but I am getting PLS 00103 error encountered ;. Please help.
Below is my trigger script,
CREATE TRIGGER trigger1 AFTER INSERT
ON Table1
FOR EACH ROW
when (new.upper(Table1.column1)='ABC')
Declare
ITEM_CODE table2.ITEM_CODE%TYPE;
BEGIN
ITEM_CODE := :new.ITEM_CODE;
INSERT INTO table2( PK,ITEM_CODE,EVENT_NUMBER)
VALUES(EVENT_NUMBER_SEQ.NEXTVAL, ITEM_CODE,EVENT_NUMBER_SEQ.NEXTVAL);
END;
I am executing this script through Toad.
Thanks in Advance.
problem is here: when (new.upper(Table1.column1)='ABC') should be (when upper(new.column1) ='ABC')
Also you don't need declare variable only to assign value. Just use value directly.
CREATE TRIGGER trigger1 AFTER INSERT
ON Table1
FOR EACH ROW
when (upper(new.column1) ='ABC')`
BEGIN
-- IF (upper(:new..column1) ='ABC') THEN
INSERT INTO table2( PK,ITEM_CODE,EVENT_NUMBER)
VALUES(EVENT_NUMBER_SEQ.NEXTVAL, :new.ITEM_CODE,EVENT_NUMBER_SEQ.NEXTVAL);
--END IF;
END;
I'm trying to insert a data into oracle database(version 11g xe).But when i'm trying to execute the procedure using toad i'm getting the error as 'ORA-01403: no data found'.
Here's my code
CREATE OR REPLACE PROCEDURE ACTSINFO.sp_Insert_WorkDetails
(p_workname IN varchar ,
p_workaddress IN varchar)
IS
BEGIN
insert into workdetails (workname,workaddress) values (p_workname,p_workaddress);
END sp_Insert_WorkDetails;
I tried to execute the procedure using the below statememt
EXEC sp_Insert_WorkDetails('test','test');
Also i've defined a trigger and sequence for the autoincrement of workdetailsid in table workdetails
Sequence is as follows
ALTER SEQUENCE ACTSINFO.WORKDETAILS_WORKID_SEQ
INCREMENT BY 1
MINVALUE 0
MAXVALUE 9999999999999999999999999999
NOCACHE
NOCYCLE
NOORDER
Trigger is as follows
DROP TRIGGER ACTSINFO.WORKDETAILS_INSERT;
CREATE OR REPLACE TRIGGER ACTSINFO.WORKDETAILS_INSERT
BEFORE INSERT
ON ACTSINFO.WORKDETAILS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
SELECT WORKDETAILS_WORKID_SEQ.NEXTVAL INTO :NEW.WORKID FROM WORKDETAILS;
END;
I'm new to oracle.Pls help me...
Your trigger is the problem:
SELECT WORKDETAILS_WORKID_SEQ.NEXTVAL INTO :NEW.WORKID FROM WORKDETAILS;
If there are no rows in workdetails the select will return nothing. Even worse, if your workdetails table has more than one row this will also fail miserably.
You really want the following:
SELECT WORKDETAILS_WORKID_SEQ.NEXTVAL INTO :NEW.WORKID FROM dual;
or - if you are on 11g - then you can use:
:NEW.WORKID := WORKDETAILS_WORKID_SEQ.NEXTVAL;