Before insert Trigger and SQL Loader

Hi,
I have a table with a BEFOR insert on each row trigger . I am loading the data to that table using sqlloader and I want my trigger to fire for each row. But the trigger is firing only for alternate rows.
body of trigger as follows.
select max(pd_id) into VarVal from <table name> where tar_id=:new.tar_id;
select pd_id+1 into :new.pd_id from dual;
if I remove the first select statement from my trigger , then it is firing for all the rows. But the removel of first select statement will not satisfy my requirements.
So please suggest me how can I achieve this.

[url http://forums.oracle.com/forums/thread.jspa?threadID=587783&tstart=0]duplicate thread

Similar Messages

  • 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

  • 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.

  • Clob field not working in BEFORE INSERT trigger

    Hello, everyone. I am trying to create a before insert trigger for a table (10g). I would like to create a value for a table field based on the xml query from a clob field. The table looks like this:
    SQL> desc lac_costing_upload
    Name Type Nullable Default Comments
    UPLOAD_ID NUMBER Y
    EMP_NUM VARCHAR2(20)
    UPLOAD_DATE DATE Y
    YEAR_MONTH VARCHAR2(10) Y
    UPLOAD_TEXT CLOB Y
    The trigger code looks like this:
    create or replace trigger lac_cst_upl_yr_month
    before insert on lac_costing_upload
    for each row
    declare
    l_month varchar2(20);
    l_year varchar2(20);
    l_error_msg varchar2(255);
    begin
    SELECT XMLTYPE (:old.upload_text).EXTRACT('Oracle_Extract/Year/text()').getstringval() Extr_Year
    , XMLTYPE (:old.upload_text).EXTRACT('Oracle_Extract/Month/text()').getstringval() Extr_Month
    into l_year
    , l_month
    FROM dual;
    :new.year_month := l_year || '.'
    || substr(l_month, 1, 3)
    exception
    when others then
    l_error_msg := substr(sqlerrm, 1, 10);
    :new.year_month := l_error_msg;
    end lac_cst_upl_yr_month;
    Here is a sample insert statement:
    insert into lac_costing_upload lcu values(2006, '008925', sysdate, null
    , '<Oracle_Extract>' || chr(10)
    || ' <Emp_Num>05417</Emp_Num>' || chr(10)
    || ' <Emp_Name>Fitzgerald</Emp_Name>' || chr(10)
    || ' <Year>2007</Year>' || chr(10)
    || ' <Month>May</Month>' || chr(10)
    || ' <Vehicle>1330</Vehicle>' || chr(10)
    || '</Oracle_Extract>'
    My hope was to see '2007.May' in the year_month field. However, I'm getting 'ORA-06502:' instead. It looks like the value of :old.upload_text is null in the trigger.
    Can anyone tell me what I'm doing wrong?
    Thanks for any help.
    --Dave                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    That's probably a really dumb error, but I don't work much with triggers.
    It's working now.
    Thanks for your help.
    --Dave                                                                                                                                                                                                                                                               

  • 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

  • How to insert BLOB datatype image using PL/SQL Procedure and SQL Loader

    Hi,
    How to insert an image into database using PL/SQL Procedure and also how to insert using SQL Loader. Please help by giving sample code and process description.
    Thanks,
    Vijay V

    http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:232814159006

  • Before delete trigger and foreign key relationship

    Hi,
    I am analysing one database for migration. On one parent table there is before delete trigger , to delete records from child. Also there is foreign key relationship on child table for this parent table.
    When I am deleting a row from parent, message gets displayed as "there are child records found."
    I would like to know, if there is foreign key relatioship then delete trigger on parent does't work, what is exactly happening?

    Could you post that trigger code and the Oracle version as well?
    With basic assumptions, I can't reproduce what you have stated here.
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> create table parent (id number primary key);
    Table created.
    SQL> create table child (id number);
    Table created.
    SQL> alter table child add constraint fk_parent foreign key (id) references parent;
    Table altered.
    SQL> create or replace trigger bdr_parent
      2  before delete on parent
      3  for each row
      4  begin
      5  delete from child where id = :old.id;
      6  end;
      7  /
    Trigger created.
    SQL> insert into parent (id) values (1);
    1 row created.
    SQL> insert into child (id) values (1);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> delete from parent where id = 1;
    1 row deleted.
    SQL> select * from parent;
    no rows selected
    SQL> select * from child;
    no rows selected
    SQL> rollback;
    Rollback complete.
    SQL> alter table child drop constraint fk_parent;
    Table altered.
    SQL> alter table child add constraint fk_parent foreign key (id) references parent on delete cascade;
    Table altered.
    SQL> delete from parent where id = 1;
    delete from parent where id = 1
    ERROR at line 1:
    ORA-04091: table SCOTT.CHILD is mutating, trigger/function may not see it
    ORA-06512: at "SCOTT.BDR_PARENT", line 2
    ORA-04088: error during execution of trigger 'SCOTT.BDR_PARENT'
    SQL>

  • How to delete the Table Contents before inserting records into SQL table ?

    Hello Experts,
            I have a scenario where in I have to Pick up some records from SAP RFC & insert into SQL table.
            i know how to do this scenario but the proble with this is before inserting we first have to ZAP the SQL table & insert a new records. One more twist is The Triggering is happening from SAP side with Sender RFC. If this would have been from SQL Side i could have written a Stored Procedure/ Trigger & could have called this before the SENDER JDBC communciation channel picks up the Triggering event from SQL side.
    So how to do this scenarioin XI, First deleting all the Records of SQL table & then inserting the new reocrds. without using the BPM.
    Regards,
    Umesh

    hi umesh,
    you can achieve this by writing SQL query in message mapping level..
    refer this link:
    http://help.sap.com/saphelp_nw04/helpdata/en/b0/676b3c255b1475e10000000a114084/frameset.htm
    regards.

  • Calling function in before insert trigger

    HELLO,
    **TRIGGER IS NOT DISPLAYING THE DATA ACCURATELY**
    **PLZ HELP ITS URGENT...**
    THE CODE IS BELOW.....
    create or replace TRIGGER GENSTUDYID
    BEFORE INSERT
    ON ER_PATPROT
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    DECLARE
         lPatStudyID varchar2(30);
         lSiteCode number(10);
         lStudyCode number(10);
         lpk_patprot     Number(10);     
         lpatprot_enroldt date;
         lFK_PER number(20);
         lIPADD varchar2(15);
         lfk_protocol number(20);
         llast_modified_by number;
         lgencal_status number;
    BEGIN
         lSiteCode := :New.fk_site_enrolling;
         lStudyCode := :new.fk_study;
         lpk_patprot := :new.pk_patprot;
         lpatprot_enroldt := :new.patprot_enroldt;
         lFK_PER := :new.fk_per;      
         lIPADD :=NULL;                
         lPatStudyID:=GenerateStudyID(lSiteCode,lStudyCode,lFK_PER);
         IF lPatStudyID IS NOT NULL then
              :NEW.patprot_patstdid := lpatstudyid;
         END IF;
    END;
    -----------FUNCTION CODE STARTS
    create or replace
    function GenerateStudyID (sitecode number,studycode number,pid number) RETURN VARCHAR IS
         lfield_name varchar2(10);
         lfield_format varchar2(10);
         lfield_length number(2);
         lfield_deftext varchar2(500);
         lparam1 varchar2(30):=sitecode;
         lparam2 varchar2(30):=studycode;
         lresult varchar2(30);
         lstudyid varchar2(30);
         lPATPROT_PATSTDID     varchar2(20);
    CURSOR studyid_def_cur is select field_name, field_format, field_length, field_deftext from bayatree.studyid_definition
    where study_code = studycode and site_code=sitecode order by field_seqno;
         CURSOR patprot_patstdid_cur is select PATPROT_PATSTDID from eres.er_patprot where fk_study=studycode and fk_per=pid;
    BEGIN
         lstudyid := NULL;
    dbms_output.put_line('studycode: '||studycode);
         dbms_output.put_line('pid; '||pid);
         FOR disp_studyid in patprot_patstdid_cur
         LOOP
              lPATPROT_PATSTDID:=disp_studyid.PATPROT_PATSTDID;
              dbms_output.put_line('lPATPROT_PATSTDID: '||lPATPROT_PATSTDID);
         END LOOP;
              IF lPATPROT_PATSTDID IS NULL THEN
              OPEN studyid_def_cur;
              LOOP
              FETCH studyid_def_cur INTO lfield_name, lfield_format, lfield_length, lfield_deftext;
              EXIT WHEN studyid_def_cur%NOTFOUND;
    lfield_deftext:= replace(lfield_deftext,'param1',lparam1);
              lfield_deftext:= replace(lfield_deftext,'param2',lparam2);
         if (instr (lfield_deftext, 'select',1) > 0) then
                   execute immediate lfield_deftext into lresult;
              else
                   execute immediate lfield_deftext;
                   lresult := null;
              end if;
              if lfield_format is not null and lresult is not null and lfield_length is not null then
                   lresult := lpad(lresult, lfield_length, 0);
              end if;
              if lfield_name is not null and lresult is not null then
                   lstudyid := lstudyid || lresult;
              end if;
         END LOOP;
         return (lstudyid);
         ELSE
              lstudyid:=lPATPROT_PATSTDID;
              Dbms_output.put_line('Study Id: '||lstudyid);
              return (lstudyid);
         END IF;
    END;

    Your post is not quiet readable, please include your program in tags, Include sample data and sample output, table and other objects creation and insert scripts.
    What error are you getting anyway post the Oracle error code
    SS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Oracle RAC and sql loader

    hi all,
    We have Oracle 10g RAC on IBM AIX
    Today i will be uploading data in a table through sql loader.
    I want that at the time of insertion only data which i am inserting goes into the table,As i am using sequence on particular column A.
    Problem is If i shutdown database then i will not to able to insert data and if i doesnt shutdown db value for that column A is generated automatically when application works.
    i have recommanded that we should stop connectivity of application with database then i will perform insertion but some people are saying no.Find other method
    What steps should i do?

    Ok i thing my question was not clear
    I have table XYZ
    A B C
    1 2 3
    4 4 5
    if application is connected to database value to Coulmn is incremented
    A B C
    1 2 3
    4 4 5
    5 6 7
    i have to insert into table XYZ using sql loader giving sequence (4,1) on column A.
    But if it is connected to application it will give me error of PK.
    as Value 5 is already inserted by application so how can i stop this.
    so that i can have the max value of coulmn and start sequence
    Hope this time make u clear.
    sorry for bad english

  • Migrating from MSSQL2005: Before-Insert Trigger Generation

    The SQL Server 2005 tables with auto=incrementer primary keys result in sequence objects being defined for use in Before-Insert triggers, which is good. I see the following code in the generated triggers:
    <br>
    IF INSERTING AND :new.ofi_service_provider_id IS NULL THEN
    SELECT OFI_SERVICE_PROVIDER_ofi_servi.NEXTVAL INTO v_newVal FROM DUAL;
    -- If this is the first time this table have been inserted into (sequence == 1)
    IF v_newVal = 1 THEN
    --get the max indentity value from the table
    SELECT NVL(max(ofi_service_provider_id),0) INTO v_newVal FROM OFI_SERVICE_PROVIDER;
    v_newVal := v_newVal + 1;
    --set the sequence to that value
    LOOP
    EXIT WHEN v_incval>=v_newVal;
    SELECT OFI_SERVICE_PROVIDER_ofi_servi.nextval INTO v_incval FROM dual;
    END LOOP;
    END IF;
    </br>
    I'm guessing this is a boilerplate template. I'd like to replace this with my own template code, so the generated triggers meet our own in-house specifications. And that looping method of bumping up a sequence object's NEXTVAL is hideous!
    Is there a way to modify your template with my own, so I can generate the triggers according to our needs?
    - Dan Clamage

    I'm trying to generate the code to fit our shop. These Before-Insert triggers would need to be rewritten for our environment. The template provided doesn't suit our needs. It'd make more sense if it was picked up from a text file, or at least presented on a Preferences screen, with placeholders for the sequence object name. Then a developer could tweak it to suit their own shop's needs, and have it generated correctly from the migration.
    This migration isn't a one-time deal for us. We'd be doing it over and over.

  • Request: PL/SQL, External Table and SQL Loader

    I see lately Questions asked in SQL and PL/SQL forum regarding SQL Loader and External Table are being moved to {forum:id=732}.
    Being an PL/SQL developer for some time now i feel External Table (and if i may, SQL Loader and DBMS_DATAPUMP) are very much an integral part of a PL/SQL development and the question related to these topics are well suited in SQL and PL/SQL forum. Even in SQL and PL/SQL FAQ we have exclusive content that discuss on these topics {message:id=9360007}
    So i would like to request the moderators to consider not moving such questions out of the SQL and PL/SQL forum.
    Thanks,
    Karthick.

    Karthick_Arp wrote:
    I see lately Questions asked in SQL and PL/SQL forum regarding SQL Loader and External Table are being moved to {forum:id=732}.
    Being an PL/SQL developer for some time now i feel External Table (and if i may, SQL Loader and DBMS_DATAPUMP) are very much an integral part of a PL/SQL development and the question related to these topics are well suited in SQL and PL/SQL forum. Even in SQL and PL/SQL FAQ we have exclusive content that discuss on these topics {message:id=9360007}
    So i would like to request the moderators to consider not moving such questions out of the SQL and PL/SQL forum.
    Thanks,
    Karthick.Not sure which moderators are moving them... cos it ain't me. I'm quite happy to leave those there.

  • Intermedia and SQL*Loader

    I created a small test table to try and figure out the syntax for the control file for SQL*Loader. I'm using what I found on technet but I can't seem to get past ->
    ** SQL*Loader-416: SDF clause for field LOCALDATA in table QUERY_INTERMEDIA references a non existent field.**
    All the files, control file, .bat file and front.tif file all exist in the same directory. I was able to get this to work when the column type was a blob but can't seem to get this right for interMedia.
    create table bsw.query_intermedia
    customer_id number not null,
    image_front ORDSYS.ORDImage
    LOB (image_front.source.localData) STORE AS
    (tablespace images_lc1 storage (pctincrease 0)
    pctversion 0 chunk 8K NOCACHE LOGGING DISABLE STORAGE IN ROW)
    pctfree 0
    Control file
    LOAD DATA
    INFILE *
    INTO TABLE query_intermedia
    TRUNCATE
    FIELDS TERMINATED BY ','
    (customer_id integer external,
    image_front column object
    (source column object
    localdata_fname FILLER CHAR(128),
    localdata LOBFILE(image.source.localdata_fname)
    mimetype char(40)
    BEGINDATA
    1,front.tif,image/tif
    .bat file to initiate sqlldr.
    sqlldr userid=bsw/bsw control=control_intermedia.ctl log=test_intermedia.log rows=300 bindsize=20000000 readsize=20000000
    Any help or suggestions would be much appreciated. Thanks.
    null

    User,
    There's a [url http://forums.oracle.com/forums/forum.jspa?forumID=260]SQL Developer Forum that's probably better suited for SQL Developer enhancement requests.
    John

  • Oracle Replication and SQL*Load direct Path

    We are setting up Oracle replication and have a few tables which are loaded using SQL*Loader Direct path. I have following questions:
    1. Can MultiMaster replication replicate direct patch sqlloaded data.
    2. If the answer to the above question is no, should we set up a new snapshot replication group . All other tables are in the Multi Master replication.
    3. Another question is on the number of replication groups. How many of these we should create. We have total of about 500 tables and database size is about 450 Gig. We plan to replicate all the tables and the refresh interval we want to be one minute for all the tables. Does having more/less replication groups help??
    Thanks for your help

    We are setting up Oracle replication and have a few tables which are loaded using SQL*Loader Direct path. I have following questions:
    1. Can MultiMaster replication replicate direct patch sqlloaded data.Yes, I don't think it should matter how the table is getting updated.
    2. If the answer to the above question is no, should we set up a new snapshot replication group . All other tables are in the Multi Master replication.see above
    3. Another question is on the number of replication groups. How many of these we should create. We have total of about 500 tables and database size is about 450 Gig. We plan to replicate all the tables and the refresh interval we want to be one minute for all the tables. Does having more/less replication groups help??
    Thanks for your help I believe what Oracle recommends is that you split up your tables into replication groups transactionally. I personally have 6 replication groups supporting 700+ tables. They are broken up more by business function than by number and size.

  • Need help:Scripting on windows and Sql Loader

    Hi
    I need some help regarding script work on windows machines i.e., how to write a script,the file extension,how to run the script etc,how to schedule it...
    I also need the doc regarding sql loader(complete details.....)
    Thanks in advance...
    bye

    The sqlldr utility is fully documented in the Oracle version# Utilities manual for your version of Oracle. You can find the documentation sets here on OTN for download or you can use the online versions at http://tahiti.oracle.com
    There is a product called Script Works and it is documented by the vendor. Otherwise the details depend on the scripting language/tool you use: DOS, Perl, etc....
    HTH -- Mark D Powell --

Maybe you are looking for