Using SQL Loader to insert data based on conditionally statements

I would like to use sql loader to insert records from a single text file into an Oracle database table. Sounds easy but the problem is that I need to check the existence of data in another table (Table A) before data can be loaded into Table B.
Table A has two columns: dept_no (primary key value) and dept_name (char)
Table B has five columns: emp_no (primary key value), dept_no (foreign key value to Table A), employee (char), job_title (char) and salary (number)
Text File looks like this:
Finance Jones President 10000
HR Smith Admin 2000
HelpDesk Jenkins Technician 3000
Problem:
1. I need sql loader to insert records into Table B by first checking if the first field in the file is in Table A.
2. If value exists get it's dept_no value.
3. If value doesn't exist discard record ( I might want to have sql loader insert a new record for this into Table A)
4. Using value from #2, insert the value in Table B for dept_no column.
5. Also assign a sequence value for the emp_no value in Table B.
Any guidance is greatly appreciated.
Thanks,

Hello,
I am not sure this is possible with SQL loader. I would rather use an external table based on your file.
Then, I would use SQL to load your data into your table, based on your conditions.
Your request is not very complicated, writing the SQL to do that will be really more simple than trying to do it with SQL loader.
Hope it will help.
Regards,
Sylvie

Similar Messages

  • How to use SQL*LOADER to read data in |SMITH|ALFRED| format

    the data I need to upload to table using SQL*LOADER is in format as below:
    |AD |Argentina  |
    |CN |China       |
    |US |America            |
    |GB |England        |so how should I write my control file to read the data into two columns table?
    I googled and people say that use Enclosed fields—delimiter (|), and I tried with below, but not working:
    LOAD DATA
    INFILE *
    insert
    INTO TABLE tmp_country_mapping
    FIELDS ENCLOSED BY '|'
    (country_id, country_name)
    BEGINDATA
    |AD |Argentina  |
    |CN |China       |
    |US |America            |
    |GB |England        |Anyone could help?
    BTW, I don`t want the spaces behind the value of the second column.
    Thanks
    Edited by: PhoenixBai on Dec 28, 2010 2:50 PM

    Problem solved, by removing the NLS_LANG=ENGLISH.
    >
    before the load, data is as below:
    |AD |安道尔共和国         |
    |AE |阿联酋            |
    |AF |阿富汗            |
    |AG |安提瓜和巴布达        |
    |AI |安圭拉岛           |
    |AL |阿尔巴尼亚          |after the load, data displays as below:
    AD      °2μà??12oí1ú
    AE      °¢áa??
    AF      °¢??o1
    AG      °2ìá1?oí°í2?′?
    AI      °21?à-μo
    AL      °¢??°í?á??And my database supports Chinese words and it displays correctly for other tables, except this one.
    What could be the problem?
    >
    Edited by: PhoenixBai on Dec 28, 2010 5:05 PM

  • Using sql Loader to insert from table?

    Hi ,,
    Can I use the SQL Loader tool to insert data from table to table, I want it to be instead of the following sql script:
    DELETE FROM TABLE1 WHERE PK IN (SELECT PK FROM TABLE2);
    INSERT INTO TABLE1 SELECT * FROM TABLE2;
    COMMIT;
    the purpose of using such method is to get the benefit of SQL Loader tool Features (log file with details, insert the correct records , ...... etc)
    Database: 9i
    O/S: Windows 2000 Server
    Edited by: eng. Habeeli on Jan 30, 2011 9:05 AM

    thanks all,
    I know that I cannot but just I wanna make sure form your experiences.
    BTW, the following method:
    DELETE FROM TABLE1 WHERE PK IN (SELECT PK FROM TABLE2);
    INSERT INTO TABLE1 SELECT * FROM TABLE2;
    COMMIT;
    Lacking the ability of loading some rows and just ignore rows with violations, and also lacking the feature of recording a log for each violation record.
    this method will stop once am error raise up and roll back all inserted record.
    so any suggestions to get such a result??

  • SQL*Loader to insert data file name during load

    I'd like to use a single control file to load data from different files (at different times) to the same table. I'd like this table to have a column to hold the name of the file the data came from. Is there a way for SQL*Loader to automatically do this? (I.e., as opposed to running an update query separately.) I can edit the control file before each load to set a CONSTANT to hold the new data file name, but I'd like it to pick this up automatically.
    Thanks for any help.
    -- Harvey

    Hello Harvey.
    I've previously attempted to store a value into a global/local OS variable and use this within a SQL*Loader control file (Unix OS and Oracle versions between 7.3.4 and 10g). I was unsuccessful in each attempt and approach I could imagine. It was very easy to use a sed script to make a copy of the control file, changing a string within it to do this however.
    Do you really want to store a file name on each and every record? Perhaps an alternative would be to use a relational model. Create a file upload log table that would store the file name and an upload # and then have the SQL*Loader control file call a function that would read that table for the most recent upload #. You'll save some disk space too.
    Hope this helps,
    Luke

  • Insert data based on condition

    I have 2 tables i need to insert data if total of 2 columns becomes 0.One value is negative and another values is positive.
    desc Crdetail
    acc_no---number(6),
    cr_Dt---date,
    cr_amt---Number(15,3)
    desc dbDetail
    acc_no----number(6),
    db_dt--date,
    db_amt--number(15,3)
    CrData
    acc_no----cr_Dt-------cr_amt
    101------10-aug-09----10
    102------20-aug-09----15
    103---21-aug-09---- -20 Note its -20
    DbData
    101----11-aug-09---- -10 --- Note Its -10
    102------20-aug-09----15
    When i inserting to other table
    the sum of db_amt and cr_amt should be zero then only it should insert.
    rgds
    ramya

    user11243021 wrote:
    Hi Instead of adding new column tot_amt can we use case statment or write a function to check and insert.just declare a variable called tot_amt and it should solve your problem. You can use if or case statement as well but I will not advise you to use a function because it becomes too costly in this case (making call then returning value).
    Rgds.

  • Loading leap year date using SQL*Loader

    Hello,
    I have a problem loading a date '29/02/2000' using SQL*Loader. This date is on a leap year. I'm getting an error message from SQL*Loader as 'ORA-01839: date not valid for month specified'. My colleague and I have tried using various date functions to convert the data into date, but no luck.
    I would appreciate any helps,
    Bruce

    Thanks for your help, I found the bug on my control file. I was using the RTRIM function to remove bad timestamp such as '29/02/2000 0:00:00'. So instead of using this statement:
    LOG_DATE DATE "DD/MM/RRRR" "RTRIM(:LOG_DATE,'0:00:00')"
    I was using the statement below with a space before the '0:00:00' string literal, with the intention to remove a space also:
    LOG_DATE DATE "DD/MM/RRRR" "RTRIM(:LOG_DATE,' 0:00:00')"
    Well, it turned out that if there was a space before the string literal, RTRIM function would trim the matching string plus any '0' characters from the right, including the '000' that belongs to '2000'. Thus, the error.
    Thanks again,
    Bruce

  • Using SQL Loader script in a Stored Procedure

    Can I use SQL Loader script in a stored procedure and then execute it from a front-end appl.? The reason for this seemingly convoluted solution is that the users don't want a batch load though the records volume is quite high (around 1 mil). Other loads using ODBC connection or OLE DB seem to be inferior to SQL Loader.

    I would suggest a couple of solutions:
    1. Have a cgi script that can upload the file to the server from a web ui, then have the cgi script call the sql*loader file, and it will insert into the database.
    2. You can try to use External tables. This is avaliable in 9i and onwards. You will be able to make any sql DML on the external table.
    I would normally use sql*loader, move the data to a staging table with nologging, and paralle loading. After it has been loaded into the staging table I would process it into my main tables. Have used this approach with up to 60 million records in one load.
    You can do calls to C procedures, Pro*C procedures through PLSQL, as well as java calls, or use Java stored procedures.
    My experience is that SQL*Loader is the fastest way to load data into the database.

  • ORA-01841 Error when value for date col is NULL in .dat (using SQL Loader)

    Hello Gurus,
    I have some data in .dat file which needs to be loaded into oracle table. I am using SQL * Loader to do the job. Although "NULLIF col_name =BLANKS" works for character datatype, but when value for date col is NULL then I get ORA-01841 error. I have to make NULL for all rows withour value for date column
    Early reply will be highly appreciated
    Farooq

    Hi,
    May be this problem is not with the NULLIF. The value for the date column is not in proper date format.
    create table:
    create table kk (empno number, ename varchar2(20), deptno number, hiredate date)
    Control file:
    LOAD DATA
    INFILE 'd:\kk\empdata.dat'
    insert into TABLE kk ( empno position (1:2) integer external,
    ename position(4:5) char NULLIF ename=BLANKS,
    deptno position (7:8) integer external NULLIF deptno=BLANKS,
    hiredate position (10:20) date NULLIF hiredate=BLANKS)
    data file:
    10 KK 01-jan-2005
    20 10
    SELECT * FROM KK;
    EMPNO ENAME DEPTNO HIREDATE
    10 KK 01-JAN-05
    20 10
    Verify the data file.
    Hope it will help

  • Insert stmt depends on condiotion using SQL*loader

    Hi everbody
    insert stmt depends on condiotion using SQL*loader
    I have problem regarding to SQL*loader
    i wants to insert data into table depeds on condiotion like
    LOAD DATA
    APPEND
    INTO TABLE TMP
    WHEN (26:33) = '161M1099'
    TMP_FIELD1 CHAR(256),
    TMP_TTYNO CONSTANT'1232:1228'
    INTO TABLE TMP
    WHEN (26:33) = '161P1340'
    TMP_FIELD1 CHAR(256),
    TMP_TTYNO CONSTANT'1232:1228'
    INTO TABLE TMP
    WHEN (26:33) = '161T1001'
    TMP_FIELD1 CHAR(256),
    TMP_TTYNO CONSTANT'1232:1228'
    it is going to 1st condition and insert both tmp_field1 and tmp_ttyno now
    problem occur when its going into 2nd condition
    it is going to insert only constant tmp_ttyno not tmp_field1
    and same in 3rd condition also
    so what to do to insert data from file in tmp_field1
    if any1 knows then please help me.

    You try with external table. A lot of flexibility is there.
    Thanks.

  • How to Load Arabic Data from flat file using SQL Loader ?

    Hi All,
    We need to load Arabic data from an xls file to Oracle database, Request you to provide a very good note/step to achieve the same.
    Below are the database parameters used
    NLS_CHARACTERSET AR8ISO8859P6
    nls_language american
    DB version:-10g release 2
    OS: rhel 5
    Thanks in advance,
    Satish

    Try to save your XLS file into CSV format and set either NLS_LANG to the right value or use SQL*Loader control file parameter CHARACTERSET.
    See http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/ldr_control_file.htm#i1005287

  • Error in loading data using SQL loader

    I am getting a error like ‘SQL*Loader -350 syntax error of illegal combination of non-alphanumeric characters’ while loading a file using SQL loader in RHEL. The command used to run SQL*Loader is:
    Sqlldr userid=<username>/<password> control =data.ctl
    The control file, data.ctl is :
    LOAD data
    infile '/home/oraprod/data.txt'
    append  into table test
    empid terminated by ',',
    fname terminated by ',',
    lname terminated by ',',
    salary terminated by whitespace
    The data.txt file is:
    1,Kaushal,halani,5000
    2,Chetan,halani,1000
    I hope, my question is clear.
    Please revert with the reply to my query.
    Regards

    Replace ''{" by "(" in your control file
    LOAD data
    infile 'c:\data.txt'
    append  into table emp_t
    empid terminated by ',',
    fname terminated by ',',
    lname terminated by ',',
    salary terminated by whitespace
    C:\>sqlldr user/pwd@database control=c.ctl
    SQL*Loader: Release 10.2.0.3.0 - Production on Wed Nov 13 10:10:24 2013
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    Commit point reached - logical record count 1
    Commit point reached - logical record count 2
    SQL> select * from emp_t;
         EMPID FNAME                LNAME                    SALARY
             1 Kaushal              halani                     5000
             2 Chetan               halani                     1000
    Best regards
    Mohamed Houri

  • Load Data from SQL Server to Oracle 10g using Sql*loader utility

    I am trying to lod data from sql server 2005 to oracle 10g.
    What is the best way to do it?
    Can sql*loader utility do it?
    what is the difference of using sql*loader utility and migration tool fom sql developer ?
    Thanks
    Edited by: user11313758 on Sep 30, 2009 4:30 PM

    Hello:
    You could consider using Oracle Heterogeneous Services to do this. If your Oracle database is on a Windows platform the link below shows you how to make a connection to SqlServer from an Oracle database.
    http://www.databasejournal.com/features/oracle/article.php/3442661/Making-a-Connection-from-Oracle-to-SQL-Server.htm
    Varad

  • How to export&import data using sql *loader

    Hi all,
    How to export&import data from sql*loader. Give me the clear steps..
    Thanks in Advance

    Hi did you already exported data from SQL SERVER? if not using SQL*LOADER you cannot export data. SQL*LOADER is only mean for importing data from flat files(usually text files) into ORACLE tables.
    for importing data into oracle tables using sql*loader use below steps
    1) create a sql*loader control file.
    it looks like as follows
    LOAD DATA
    INFILE 'sample.dat'
    BADFILE 'sample.bad'
    DISCARDFILE 'sample.dsc'
    APPEND
    INTO TABLE emp
    TRAILING NULLCOLS
    or for sample script of control file search google.
    2) at command prompt issue following
    $ sqlldr test/test
    enter control file=<give control file name which you create earlier>
    debug any errors (if occured)

  • How to load date column using sql loader

    Hi,
    I am trying to load a file using sql loader. my date value in the file is '2/24/2009 8:23:05 pm',
    In control file for this column i specified like this
    rec_date date ''mm/dd/yyyy hh:mi:ss pm"
    But i am getting following error
    not avalid month.
    Thanks
    sudheer

    Hi,
    Use this example as reference:
    CTL file:
    LOAD DATA
    INFILE 'test.txt'
    BADFILE 'test.bad'
    truncate INTO TABLE T3
    FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '|' TRAILING NULLCOLS
    dt_date DATE "mm/dd/yyyy hh:mi:ss pm")DAT file:
    2/24/2009 8:23:05 pm
    C:\ext_files>sqlldr hr/hr control=test.ctl
    SQL*Loader: Release 10.2.0.1.0 - Production on Wed Jul 1 20:35:35 2009
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    Commit point reached - logical record count 1
    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    SQL> desc t3;
    Name    Type Nullable Default Comments
    DT_DATE DATE Y                        
    SQL> select to_char(dt_date, 'mm/dd/yyyy hh24:mi:ss') from t3;
    TO_CHAR(DT_DATE,'MM/DD/YYYYHH2
    02/24/2009 20:23:05
    SQL> Regards,
    Edited by: Walter Fernández on Jul 1, 2009 8:35 PM - Adding example...
    Edited by: Walter Fernández on Jul 1, 2009 8:38 PM
    Edited by: Walter Fernández on Jul 1, 2009 8:41 PM - Fixing some information...

  • Loading data with dates using SQL*Loader

    Dear everyone
    I am currently trying to load some data containing dates using SQL*Loader.
    For termination of fields I have been using ^ because I have some book titles which contain " and ' as part of their title. I found that the TO_DATE function did not seem to work using ^ instead of ". Would I be correct? I think the Oracle manual says that " must be used.
    After some Web research I eventually amended my control file to as follows:
    load data
    infile 'h:\insert_statements\22_insert_into_SCAN_FILE_INFO.txt'
    REPLACE
    into table SCAN_FILE_INFO
    fields terminated by "," optionally enclosed by '^'
    TRAILING NULLCOLS
    (scan_id, scan_filename
    file_format_id
    orig_scanning_resolution_dpi
    scanner_id, scanner_operator_id
    scanning_date "TO_DATE (:scanning_date, 'YYYY-MM-DD')"
    original_map_publication_id
    reprint_publication_id)
    A simple line of data is as follow:
    280001, ^1910 - London^, 270001, 400, 250001, 260001, "TO_DATE('2007-06-06', 'YYYY-MM-DD')", 200019,
    The final column being null.
    However when I attempt that I get the following error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO, column SCANNING_DATE.
    ORA-01841: (full) year must be between -4713 and +9999, and not be 0
    If I change the scanning_date part to:
    scanning_date "EXPRESSION TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    or
    scanning_date "CONSTANT TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    I get the error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO, column SCANNING_DATE.
    ORA-00917: missing comma
    As soon as I do the following:
    scanning_date "EXPRESSION, TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    or
    scanning_date "CONSTANT, TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    I get too many values error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO.
    ORA-00913: too many values
    I also tested out scanning_date DATE "YYYY-MM-DD", but that just gave the same ORA-01841 error message as above.
    I must be doing something very simple which is wrong but I cannot figure it out.
    Kind regards
    Tim

    And why do you have scanning date as "TO_DATE('2007-06-06', 'YYYY-MM-DD')" in your infile? All you need is 2007-06-06. If you can not change infile generation code, use:
    load data
    infile 'h:\insert_statements\22_insert_into_SCAN_FILE_INFO.txt'
    REPLACE
    into table SCAN_FILE_INFO
    fields terminated by "," optionally enclosed by '^'
    TRAILING NULLCOLS
    (scan_id, scan_filename
    file_format_id
    orig_scanning_resolution_dpi
    scanner_id, scanner_operator_id
    scanning_date "TO_DATE(REPLACE(REPLACE(:scanning_date,'TO_DATE('),'''YYYY-MM-DD'')'), 'YYYY-MM-DD')"
    original_map_publication_id
    reprint_publication_id)SY.

Maybe you are looking for

  • Domain Controller cannot access \\domain\netlogon causing Auth issues

    Hi everyone, I have been spent all day trying to figure out what is going on here, I have a Domain controller (only DC in the environment) that is acting funny I first noticed when I was attempting to RDP into a server in my domain I was getting "acc

  • Field in BAPI_GOODSMVT_CREATE

    Hi , I am using BAPI_GOODSMVT_CREATE for creating GRN , In transcation MIGO , at line item level there is a field which does not exist in GOODSMVT_ITEM table.Please suggest me how to populate the field which does not exist in BAPI_GOODSMVT_CREATE. Re

  • HELP Can't install itunes 9 on Windows 7  64-Bit

    I've just bought an iPod Touch and can't install itunes on my notebook. (Which means I can't use my iPod) I've downloaded itunes for 64bit, but each time I double-click it, a window opens and say "The windows installer service could not be accessed.

  • Image Capture Boot Image Fails with 0xc000000f

    I am trying to capture a Windows 8.1 Enterprise (with Update) x64 installation using WDS, but the Capture Image fails to load. I used the boot.wim from the Windows Server 2012 R2 (with Update) ISO to create a boot image in WDS and then a capture imag

  • SQL Developer 1.5.1, JDK 5 u16 + JDK 6 u7 + u10, & Insider 2.5 = FREEZE

    Just found an issue with Insider 2.5 in SQL Developer 1.5.1 while using JDK 5 u16, JDK 6 u7 or u10 on WinXP SP3: Insider freezes SQL Developer 1.5.1 completely. Console output is as follows: java.lang.NoSuchFieldError: INSTANCE         at elephant.in