Using sequence in insert trigger?

Hi all,
I'm new to this forum, and for that matter, any forums related to computers.
I would appreciate it if anyone would give me some hinters.
I created a table with an ID column of type "Number." And I want to populate that column with a sequence number everytime I insert into that table.
I have a sequence named "sequence1_account."
Below is my insert trigger code:
create TRIGGER system.trigger1_account
BEFORE INSERT ON system.table_account
for each row
BEGIN
:new.id := sequence1_account.NextVal;
END;
Note:
user is "system"
table is "table_account"
The error that I get when I try to compile this is
PLS-00357: Table,View Or Sequence reference 'SEQUENCE1_ACCOUNT.NEXTVAL' not allowed in this context
So, does that mean I cannot use a sequence in a trigger?
Thanks in advance!
in His love,
HS

Hello,
Hoping for some help with sequence triggers as well.
CREATE TRIGGER "SCALS"."TRIGGER_CALL_NUM"
BEFORE INSERT ON "CALLLOG" FOR EACH ROW
BEGIN
select num_callers.NextVal into : new.callernumber from dual;
END;
Problem is that the trigger status is invalid ??
The error I get is in German (German installation)
Zeilen-# = 0 Spalten-# = 0 Fehlertext = PLS-00801: internal error [ph2csql_strdef_to_diana:bind]
Zeilen-# = 2 Spalten-# = 57 Fehlertext = PL/SQL: ORA-06544: PL/SQL: internal error, arguments: [ph2csql_strdef_to_diana:bind], [], [], [], [], [], [], []
Zeilen-# = 2 Spalten-# = 1 Fehlertext = PL/SQL: SQL Statement ignored
But perhaps something looks familiar???

Similar Messages

  • Problem while using Before insert Trigger

    Hello everybody,
    I am using 9.2.0.4.0 XMLDB..
    The problem occurs while using the before insert trigger shown below to convert a CLOB(:new.file_clob) to XMLTYPE(:new.file_xml). The trigger is on a table with file_clob and file_xml as columns. file_xml column is based on a registered schema.
    The trigger is:
    Create or replace trigger po_2_demo_xml_tab_trg
    before insert on po_2_demo_xml_tab
    FOR EACH ROW
    -- Step 1:to convert CLOB to XMLtype and store it
    --in :new.file_xml .
    :new.file_xml := xmltype(:new.file_clob);
    -- Step 2:to verify if :new.file_xml has been created
    dbms_output.put_line(:new.file_xml.extract('/PurchaseOrder/PONum/text()').getStringVal());
    end;
    If u insert a valid xml file, u can see the PONum value on dbms output(Step 2 of the trigger works). u also get the "1 row inserted" message.
    Surpisingly, if u query on the table it shows that the file_xml column is null! For some reason, the value of :new.file_xml in the before insert trigger is not being retained in the table.
    Has anybody encountered this before? Do I have to apply any patch?
    Appreciate any help in this regard.
    Thanks,
    Partha.

    sorry! I just noticed.. the trace file does show an error, but the error is while creating the table itself.
    Please see the segment from trace file below:
    The following statements encountered a error during parse:
    create table po_2_demo_clob_xml_tab
    file_clob CLOB,
    file_xml xmltype
    )xmltype column file_xml
    XMLSCHEMA "http://Friday/PO_2.xsd"
    ELEMENT "PurchaseOrder";
    XCTEND rlbk=0,
    Error encountered: ORA-00911
    But, inspite of this I am able to insert a xml file into file_xml using a sql statement. But the before insert trigger does not retain :new values.
    Appreciate any comments.
    Thanks,
    Partha.

  • After insert trigger with :NEW.ROWID

    Hi All,
    I am using a After insert trigger to generate history record as following:
    CREATE TABLE TB_TEST (classID number(3), classNm varchar2(12));
    CREATE TABLE TB_HIST_TEST (hist_dttm timestamp(6), classID number(3), classNm varchar2(12));
    CREATE or REPLACE TRIGGER air_test AFTER INSERT ON tb_test FOR EACH ROW
    BEGIN PK_SRVC.CRT_NewRec('TB_TEST', 'TB_HIST_TEST', :new.ROWID); END:
    In PK_SRVC package, I use the following statment to create new record in TB_HIST_TABLE:
         Insert into tb_hist_test (hist_dttm, classID, classNm)
         values (select systimestamp, classID, classNm from tb_test where rowid = ROWID )
    The trigger DOES fire when a new row is inserted into TB_TEST. However there is no record inserted into TB_HIST_TEST. Any suggestion?
    Thanks,

    The PK_SRVC.CRT_NewRec is a generic service package that can be shared by many tables. It uses dynamic SQL to get all the collumns for different tables based on USER_TAB_COLUMNS.  The following is the code of this package:
    PROCEDURE CRT_NewRec ( p_TableName IN VARCHAR2, p_AudTableName IN VARCHAR2, p_RowID)
    IS
           TYPE TabCol_RecTyp IS RECORD (COLUMN_NAME VARCHAR2(30), COLUMN_ID NUMBER);
           TYPE TabCol_CurTyp IS REF CURSOR;
            c_TabCol TabCol_CurTyp;
            rc_TabCol TabCol_RecTyp;
            v_Sql_TabCol VARCHAR2(1000);
            v_ColNames VARCHAR2(1000);
            v_Sql VARCHAR2(1000);
            PRAGMA AUTONOMOUSE_TRANSCATION;
    BEGIN
            v_SQL_TabCol := ' SELECT column_name, column_id FROM USER_TAB_COLUMNS'
                                      ||  ' WHERE table_name = ' || CHR(39) || p_TableName || CHR(39)
                                      || '  ORDER BY column_id';
            v_ColNames := NULL;
            OPEN c_TabCol FOR v_Sql_TabCol;
            Loop
                   FETCH c_TabCol INTO rc_TabCol; Exit WHEN c_TabCol%NOTFOUND:
                   v_ColNames := v_ColNames || ',' || rc_TabCol.COLUMN_NAME;
            End Loop;
            CLOSE c_TabCol;
            v_Sql := 'INSERT INTO ' || p_AudTableName || '(HIST_DTTM, ' || v_ColNames || ' )'  
                      || ' SELECT systimestamp, ' || v_ColNames || ' FROM ' || p_TableName
                      || ' WHEN ROWID = chartorowid(' || CHR(39) || p_RowId || CHR(39) || ')';
             EXECUTE IMMEDIATE v_Sql;
             COMMIT;
    END;    
    (charmingholidays-yyz)

  • Using a sequence inside BEFORE TRIGGER

    Hi all,
    I just created a testtable and a sequence to use as a primary key column value for that table.
    I tried to create a BEFORE INSERT trigger on that table and in the trigger i tried to set up the primary key column value using the sequence
    but while compiling i am getting the error "Error(9,30): PLS-00357: Table,View Or Sequence reference 'SEQ_OF_TESTTABLE.NEXTVAL' not allowed in this context"
    My Version:Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    All the objects created with the same user. I would appraciate any help, thanks all
    EDIT
    I solved the problem using the below
    create or replace
    TRIGGER Bef_Ins_On_Testtable
    BEFORE INSERT ON TestTable
    FOR EACH ROW
    declare
    ntemp_id INT;
    BEGIN
    SELECT SEQ_OF_TESTTABLE.NEXTVAL INTO ntemp_id FROM DUAL ;
    DBMS_OUTPUT.PUT_LINE('İNSERTED');
    :NEW.VSURNAME := 'HAKKİ' ;
    :NEW.NID := ntemp_id;
    END;But i wonder why i can use the sequence(just as seqeunce_name.NEXTVAL) in INSERT statement and why cant in trigger?
    Edited by: user9371286 on 31.Tem.2010 04:15
    Edited by: user9371286 on 31.Tem.2010 04:21
    Edited by: user9371286 on 31.Tem.2010 04:27

    Please post your trigger code and your database version ( the result of: select * from v$version; ).
    Put it between tags, so your example will stay formatted.
    (see: http://forums.oracle.com/forums/help.jspa for more examples regarding tags)
    "PLS-00357: Table,View Or Sequence reference "string" not allowed in this context
        Cause: A reference to database table, view, or sequence was found in an inappropriate context. Such references can appear only in SQL statements or (excluding sequences) in %TYPE and %ROWTYPE declarations. Some valid examples follow: SELECT ename, emp.deptno, dname INTO my_ename, my_deptno, my_dept .FROM emp, dept WHERE emp.deptno = dept.deptno; DECLARE last_name emp.ename%TYPE; dept_rec dept%ROWTYPE;
        Action: Remove or relocate the illegal reference."
    +http://download.oracle.com/docs/cd/B19306_01/server.102/b14219/plsus.htm#sthref13592+
    You can find examples of triggers referring to sequences here, by doing a search on this forum or:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm#ABC1032282                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to create an INSERT trigger which creates a "sequence number"

    Imagine a table T_Q with columns C_1, C_2, C_3 and UN_123.
    C_1, C_2 and C_3 are all numeric and given by the user.
    UN_123 has to be a calculated sequence number starting by 1 and incremented by 1 for each combination of C_1, C_2 and C_3, i.e., the sequence number depends on the key values C_1, C_2 and C_3.
    Could anybody provide a code sample on how to create a BEFORE INSERT trigger , which calculates the value of the column UN_123 based on the values of C_1, C_2 and C_3 ??
    Premise: Rather than using any sequence, the trigger code should only be based on the table T_Q
    null

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Rainer Wagner ([email protected]):
    Imagine a table T_Q with columns C_1, C_2, C_3 and UN_123.
    C_1, C_2 and C_3 are all numeric and given by the user.
    UN_123 has to be a calculated sequence number starting by 1 and incremented by 1 for each combination of C_1, C_2 and C_3, i.e., the sequence number depends on the key values C_1, C_2 and C_3.
    Could anybody provide a code sample on how to create a BEFORE INSERT trigger , which calculates the value of the column UN_123 based on the values of C_1, C_2 and C_3 ??
    Premise: Rather than using any sequence, the trigger code should only be based on the table T_Q<HR></BLOCKQUOTE>
    null

  • Using Database Change Notification instead of After Insert Trigger

    Hello guys! I have an after insert trigger that calls a procedure, which in turn is doing an update or insert on another table. Due to mutating table errors I declared the trigger and procedure as autonomously transactional. The problem is, that old values of my main tables are inserted into the subtable since the after insert/update trigger is fired before the commit.
    My question is how can I solve that and how could I use the change notification package to call my procedure? I now that this notification is only started after a DML/DDL action has been commited on a table.
    If you could show me how to carry out the following code with a Database Change Notification I'd be delighted. Furthermore I need to know if it suffices to set up this notification only once or for each client seperately?
    Many thanks for your help and expertise!
    Regards,
    Sebastian
    declare
    cnumber number (6);
    begin
    select count(*) into cnumber from (
    select case when (select date_datum
        from
          (select f.date_datum,
            row_number() over (order by f.objectid desc) rn
          from borki.fangzahlen f
          where lng_falle      = :new.lng_falle
          and int_fallennummer = :new.int_fallennummer
          and lng_schaedling   = :new.lng_schaedling
          and date_datum       > '31.03.2010'
        where rn=1) < (select date_datum
        from
          (select f.date_datum,
            row_number() over (order by f.objectid desc) rn
          from borki.fangzahlen f
          where lng_falle      = :new.lng_falle
          and int_fallennummer = :new.int_fallennummer
          and lng_schaedling   = :new.lng_schaedling
          and date_datum       > '31.03.2010'
        where rn=2) then 1 end as action from borki.fangzahlen
            where lng_falle      = :new.lng_falle
            and int_fallennummer = :new.int_fallennummer
            and lng_schaedling   = :new.lng_schaedling
            and date_datum       > '31.03.2010') where action = 1;
    if cnumber != 0 then
    delete from borki.tbl_test where lng_falle = :new.lng_falle
    and int_fallennummer = :new.int_fallennummer
    and lng_schaedling   = :new.lng_schaedling
    and date_datum       > '31.03.2010';
    commit;     
    pr_fangzahlen_tw_sync_sk(:new.lng_falle, :new.int_fallennummer, :new.lng_schaedling);

    It looks like you have an error in line 37 of your code. Once you fix that the problem should be resolved.

  • Question re use of sequence in INSERT statement

    Hi
    I a having a problem inserting data into a table. I have created a sequence....
    CREATE SEQUENCE ag_in_target_seq
    INCREMENT BY 1
    START WITH 1
    ;and Im trying to use it to insert an incrementing ID value into a column for newly created table. Im getting an error message with regard to the sequence.
    "The specified sequence number (CURRVAL or NEXTVAL) is inappropriate here in the statement."
    I've just started looking at sequences but not sure of my logic or syntax. (bit of a noob). If someone could point me in the right direction it would be much appreciated. Thanks a mil.
    INSERT INTO AGENTSINFOTARGET
    SELECT    
               a.code_name,
               i.information_id,
               i.gleaned_date,
               t.first_name,
               t.last_name,
               l.description,
               ag_in_target_seq.nextval
    FROM
               agents a
               INNER JOIN
               information i ON
               a.agent_id=i.agent_id
               INNER JOIN
               targets t ON
               i.target_id=t.target_id
               INNER JOIN
               locations l on
               t.location_id=l.location_id
               ORDER BY
               a.code_name desc
      ;

    Hi Sanjay
    Thanks for taking an interest, here is the full error message
    Error at Command Line:9 Column:28
    Error report:
    SQL Error: ORA-02287: sequence number not allowed here
    02287. 00000 - "sequence number not allowed here"
    *Cause:    The specified sequence number (CURRVAL or NEXTVAL) is inappropriate
    here in the statement.
    *Action:   Remove the sequence number.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Problem with forms6i using Pre-Insert Trigger

    I created a block level pre-insert trigger to do some validations and when I save the first time with wrong data it is working fine and popping up alerts that I defined but if I press save the second time it is saving even though the data is wrong.
    This is in Applications 11.5.10 using all the property classes and running on the server.
    I created exact replica of the form(no applications, property classes
    just basic form but with same functionality) and ran it on my local machine it is working fine. It is giving error till the data is fixed.
    Anybody know why?
    Thanks in advance

    It did not work. It is still doing the samething. Actually it is popping up my alerts but saving the record first time itself. Is there any way I can tell it not to save if the data is wrong. My code is below it basically pops up the alerts but doesn't tell anywhere not to save it.
    Thanks
    VJ
    declare
         v_amount number;
         alert_id      ALERT := Find_Alert('amount_from');
         alert_id1 ALERT := Find_Alert('amount_to');
         dummy_var NUMBER;
    begin
         select max(ssibe_amount_to) into v_amount from ssibe_orderappr_tab where
              ssibe_account_number = :ssibe_orderappr.ssibe_account_number;
              if :ssibe_orderappr.ssibe_amount_from <= v_amount
              then
                   Set_Alert_Property(alert_id, ALERT_MESSAGE_TEXT, 'Amount_From should be greater than '|| v_amount);
                   dummy_var := Show_Alert(alert_id);
              else
                   IF (:SSIBE_ORDERAPPR.SSIBE_AMOUNT_TO <= :SSIBE_ORDERAPPR.SSIBE_AMOUNT_FROM)
                   THEN
                   dummy_var := Show_Alert(alert_id1);
                   end if;
              end if;
    end;

  • Need help with creating trigger using instead of insert.

    Hi all,
    I am trying to create a trigger that can read the inserted Mail data from table1 and check if the Mail data matches the Mail data from table2. If the Mail data matches the Mail data from table2 it will get the EmpID from table2 and insert it into table1
    column EmpID. 
    Here are table2 columns:
    EmpID (int) Mail(varchar) Mail2(varchar)
    101 [email protected] [email protected]
    102 [email protected] [email protected]
    table1 columns 
    EmpID (int)(primary key) Mail(varchar) Mail2(varchar)
    If I insert [email protected] into table1 column Mail, I would like it to get the value for the EmpID from table2 before actually inserting the record into table1, by matching the Mail from table1 = Mail from table2.
    I am using ASP.Net to insert the records into Mail and Mail2.
    How can I achieve that?
    I appreciate any help.

    There should be two SQL statements in the stored procedure in order to accomplish the task?
    Ideally you need to include logic as a part of your insert procedure itself. You should have a standard insert stored procedure which should include this logic and should be used for all inserts.
    Also if EmpID field has to have a non NULL value always you may better off creating a foreign key constraint from Table1 to Table2 on EmpID column to enforce the Referential Integrity.
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Bulk insert using stored procedure or trigger

    Hi ,
    I have to insert around 40,000 rows in a table querying other database using database link.
    Please advice whether I do using stored procedure or trigger.
    Thanks.

    Here is a basic benchmark that illustrates the difference between maximising SQL, or doing it in PL/SQL instead.
    Care needs to be taken with such a benchmark in order for physical I/O not to negatively impact a test, and then have no impact in the second test as that data read from disk now sits in the cache.
    So I ran it a couple of times in order to "warm up" the cache. I also put the maximise-SQL test first in order to show that it is still faster, despite any physical I/O it may do (which will likely be faster logical I/O with the second test).
    Run on Oracle XE 10.2.0.1.
    SQL> drop table my_objects_copy purge;
    Table dropped.
    SQL> create table my_objects_copy as select * from all_objects;
    Table created.
    SQL>
    SQL> set timing on
    SQL> begin
    2 delete from my_objects_copy;
    3
    4 insert into my_objects_copy
    5 select * from all_objects;
    6
    7 commit;
    8 end;
    9 /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:01.11
    SQL> set timing off
    SQL>
    SQL> drop table my_objects_copy purge;
    Table dropped.
    SQL> create table my_objects_copy as select * from all_objects;
    Table created.
    SQL>
    SQL> set timing on
    SQL> declare
    2 cursor c is select * from all_objects;
    3 objRow ALL_OBJECTS%ROWTYPE;
    4 begin
    5 delete from my_objects_copy;
    6
    7 open c;
    8 loop
    9 fetch c into objRow;
    10 exit when c%NOTFOUND;
    11
    12 insert into my_objects_copy
    13 values objRow;
    14
    15 end loop;
    16 commit;
    17 end;
    18 /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:03.21
    SQL>
    The cursor-fetch-loop is almost 3 times slower. The more rows there are to process, the slower the cursor-fetch-loop will become, as it will create more and more context switching and copying data between the SQL and PL engines and back.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Sequence number in Trigger

    I'm trying to create an insert trigger that will do the following:
    1.) Grab the next sequence number from a sequence and use it as the value for the id_number column.
    2.) Grab the SYSDATE and use it as the value for the create_date column.
    I'm using Oracle 8.1.6 on HP-UX. Here's my syntax, so far:
    CREATE OR REPLACE TRIGGER tI_GRANT_TEST_CHILD before INSERT on GRANT_TEST_CHILD for each row
    declare curDate DATE;
    declare idNumber NUMBER;
    begin
         SELECT SYSDATE into curDate FROM DUAL;
         SELECT GRANT_TEST_SEQ.NEXTVAL into idNumber FROM DUAL;
         :new.CREATE_DATE := curDate;
         :new.ID_NUMBER := idNumber;
    end;
    I'm pretty new to Oracle, so I've probably got a syntax error. The trigger worked fine when I just had the create date functionality; but, I can't get the sequence stuff to work.
    Also, anyone know any good links for Oracle syntax standards?
    Thanks for the help.

    I don't see anything wrong with the syntax in your example, but the trigger can be simplified to:
    create or replace trigger ti_grant_test_child
    before insert on grant_test_child
    for each row
    begin
      select grant_test_seq.nextval
        into :new.id_number
        from dual;
      :new.create_date := sysdate;
    end;
    /You can type SHOW ERRORS after compiling in SQL*Plus to see the actual errors.

  • INSERTstatement is not working if there is a before insert trigger

    INSERTstatement is not working if there is a before insert trigger on that table. That trigger contains an insert to another table which having the main table reference.
    Let us say, for example there a table named 'EMP_DEPT' and there is a before insert trigger on this table.
    In this trigger inserting a record in to another table named 'AUDIT_EMP' and in this table using EMP_Dept primary key as foreign key.
    Table EMP_DEPT is having the below columns:
    EMP_DEPT_SYS_ID
    EMP_DEPT_NO
    DEP_NAME etc..
    Table AUDIT_EMP is having the below columns:
    AUDIT_EMP_SYS_ID
    EMP_DEPT_SYS_ID
    AUDIT_NO etc..
    the code in the trigger is
    INSERT INTO audit_emp
    (audit_emp_sys_id, emp_dept_sys_id, audit_no
    VALUES (audit_emp.NEXTVAL, :new EMP_DEPT_SYS_ID, '1101'
    Now when you execute the insert query like:
    INSERT INTO emp_dept
    (emp_dept_sys_id, emp_dept_no, dep_name
    VALUES (EMP_DEPT.nextval, 1001, 'Dep-1'
    It is giving the error saying 'Integrity constraint error, parent key not found' from the trigger.
    But, when you modify the above insert query like the below then it is working.
    INSERT INTO emp_dept
    (emp_dept_sys_id, emp_dept_no, dep_name
    SELECT EMP_DEPT.nextval, 1001, 'Dep-1'
    FROM DUAL;
    I am using Oracle 10g.
    Why the insert into values is not working. Is there any commit transaction sequence change? Any idea please?
    Edited by: user6475632 on Sep 16, 2009 7:08 AM

    Obviously the code you posted can not work.
    You are inserting the detail record (the audit record) before the master record, where it should have been after. IMO, you should fire the trigger AFTER INSERT for each row.
    If that still doesn't work, you need to change the foreign key constraint into a deferred constraint, which is evaluated at commit, instead of immediately. The SQL reference manual has further info on this.
    'Oracle 10g' is considered a marketing label here, not a -4 digit- version.
    Sybrand Bakker
    Senior Oracle DBA

  • USING SEQUENCE IN PACKAGE SPEC

    I Want to Use Sequence.NEXTVAL & Sequence.CURRVAL throughout the package. How can i declare the global sequence variable and make them available for all the stored procedures inside the package ?
    Any help highly appreciated..
    Thank you all in advance

    Data Boy wrote:
    No i know that but using directly SEQUENCE.CURRVAL inside the Insert statement is not a good practice.
    We need to declare inside the procedure a variable and then fetch the SEQUENCE.CURRVAL into that variable
    and use that variable inside the insert statement. But we that variable scope will be available only inside that procedure.
    if i want the same CURRVAL in another stored procedure. How can i get it ..that is my Question.What exactly are you trying to achieve?
    If it's a case of needing to know the value of the sequence number assigned to an inserted record you would typically use the RETURNING clause on the insert statement...
    SQL> create table temp (id number, val varchar2(20));
    Table created.
    SQL> create sequence temp_seq;
    Sequence created.
    SQL> ed
    Wrote file afiedt.buf
      1  create trigger trg_temp before insert on temp
      2  for each row
      3  begin
      4    select temp_seq.nextval into :new.id from dual;
      5* end;
    SQL> /
    Trigger created.
    SQL> set serverout on
    SQL> declare
      2    v_num number;
      3  begin
      4    insert into temp (val) values ('Fred') returning id into v_num;
      5    dbms_output.put_line('ID inserted was: '||to_char(v_num));
      6  end;
      7  /
    ID inserted was: 1
    PL/SQL procedure successfully completed.
    SQL> select * from temp;
            ID VAL
             1 Fred
    SQL>

  • Problem determining if id is present in BEFORE INSERT trigger

    Hi,
    I have written a BEFORE INSERT trigger in which I am trying to determine whether or not an id (primary key ) has been submitted as part of the insert. The reason for this is that in my application there are two cases in which the trigger will be called, 1) when the id has been previously obtained from a sequence and passed in, and 2) when an id needs to be 'automatically' generated from within the trigger'. (two subsystems which work differently talk to the database in different ways)
    this is the key part of the trigger code:
    CREATE OR REPLACE TRIGGER trg_submissions_seq_id
    BEFORE INSERT on submissions
    FOR EACH ROW
    BEGIN
    if :new.id IS NULL THEN
    select seq_submissions_id.nextval INTO :new.id FROM DUAL;
    end if;
    ....more code here
    END;
    I think this is correct. Can someone confirm this.
    However the behaviour I see is that in case 1 the id is increased twice , once when it is obtained from the sequence , and then again in the trigger... despite the condition I put in.
    If anyone can shed any light on this behaviour I would be grateful.
    Mark

    This trigger (at least what you've posted of it) won't increment the sequence if Rails passes in the ID column. If you're always seeing the sequence incremented twice, I would tend to wager that either Rails is calling the nextval function twice or that it is not passing in an ID. Of course, since Oracle sequence-generated values are not gap-free, presumably this is just a cosmetic issue, not a functionality issue.
    As a general matter, I'd suggest not using ID as a column name for a table. In addition to being a poor naming convention, ID is a reserved word.
    Justin

  • Using Sequence

    Hi Everyone,
    Is there any way that we can assign/attach a sequence to the column of a table, actually the requirement is that I have table, in which a third party will be inserting the data and they want me to attach a sequence to column where that sequence should generate a auto number upon inserting each record.
    As far as I knew that we can use sequence thru a form to insert auto numbers.
    Any help will be appreciated.
    Thanks in advance
    Ahon

    Is there a simple way to return the value generated to the form?
    Forms 6i, 9i database.
    I have a basic form that allows the user to insert a record into a table CALL_HISTORY. That table has a BEFORE INSERT trigger that gets a sequence value and uses it as the primary key, CALL_ID.
    Now on the form post-save I want the call_id display field populated with this new value. I can't query for it as none of the other data is unique. Surely Forms must have some function to do this?

Maybe you are looking for

  • How do I find out what filter effect I used on an image after I have saved it?

    I used a series of filters that gave an image a very cool painterly - impressionistic painting - quality.  I have forgot what steps I took to get that effect.  I have tried to go back and look at the image history but that did not tell me anything si

  • Connect Timesten server remotely without using timesten client

    I know it's a silly question but this is what i want to do:- I have my java application and I want to connect to the timesten server present on remote machine and moreover I don't have the permission to intall any Timesten Client or App server(weblog

  • IWeb 2009 - Crash & Domain.Site possible corruption?

    I was working on a webpage that I publish to my MobileMe account. Created the Welcome page, no problem, when I got to the "My Albums" is when my crashing problem begain. Something regarding a Kernal Exception (SGID). These were the steps: Created Alb

  • Network routers clashing? BEFW11S4 and WRT54GS

    I have a small network. I have a splitter followed by a router (IP Address 192.168.1.1) and branching of in to several separate sub networks. In one of these sub networks I have a problem; at times it refuses to connect to the Internet. The set-up is

  • Library has disappeared

    For some reason my iTunes library is no longer appearing when I open iTunes. I get a message that my iPod is synced with another iTunes library. How do I find my iTunes library?