Tricky SQL query to DUMP to flat file

Hi All,
I have used UTL file utility before to dump table data into a comma seperated file but this time I have a tricky problem.
I have a table called customers with the following fields
work_order_no, customer_no, contract_start_date, title, Name, first_name, street, street_no, bank_code, name, liason_name
and have a products table with following fields
work_order_number, material_type, material_number, qty
1. I need to write the customer details + product details of the customer into a comma seperated file as below
A
1001,
1234,
12.01.2004,
Mr. Dr.,
Alphs,
Thomas,
my street,
12,
1112,
Barclays,
Mr. Dr. Alphs Thomas,
<EOH> - is a fixed
1001,
M,
12,
1,
<EOL> is fixed
1001,
M,
12,
1,
<EOL> is fixed
1001,
M,
12,
1,
<EOF> is fixed
4 - total number of records
1 - total number of A type of records(in the first position
How do i write a Pl/Sql code which can be called from command prompt.
Thanks in advance

You can try this out and see if it works :
CURSOR to fetch the customers data
Loop
- Use a string to concatenate the cursor variables
- Use chr(10) to print the columns in seperate lines
i.e. lResult := x.work_order_no || ',' || chr(10)
|| x.customer_no || ',' || chr(10)
|| x.contract_start_date || ',' || chr(10)
|| x.liason_name || ',' || chr(10)
- Use Utl_File to write the formatted string to
the output file
- Use Utl_File to write constant '<EOH>' to the
output file
- Get all product details
- For each product details
Loop
lResult := x1.material_type || ',' || chr(10)
|| x1.material_number || ',' || chr(10)
|| x1.Qty || ',' || chr(10)
- Use Utl_File to write the formatted
string to the output file
- Use Utl_File to write constant '<EOL>' to
the output file
End Loop
End Loop
- Use Utl_File to write constant '<EOF>' to the output
file
- Use Utl_File to write counters to the output file
You can write the code in a PL/SQL script file called a.sql and save it on the server directory 'c:\a.sql'.
To run from the command prompt type:
c:\> sqlplus userid/pwd@connectstring @c:\a.sql
Hope this helps.
-- Shailender Mehta --

Similar Messages

  • Query extraction to a flat file

    We have a requirement to extract the query result and send it as a flat file to a different system. The query is created on a multi provider and the key figures have lot of restrictions. We currently use RSCRM_BAPI for this. Would like to know if there is a better options, if any one has used it.
    Thanks,
    VJ Sudharsan

    Hi,
    It might help you:
    Program to run query and save to flat file
    Best regards,
    Eugene

  • Saving query results to a flat file

    Hello Experts!
    We have a very specific issue on our current project and I would like to know if any of you have ever done something similar. We are taking query results from BW (after complex calculations, some based on SY-DATE) and saving them to flat files to transfer to a SQL database structure on the Enterprise Portal. From here, the portal team renders the information into more "static" dashboards that include mouse over features and even limited drilldown (i.e. no matter where a user clicks the report always drills down on profit center)
    There are many reasons why the model is set up as such (mostly training of executive level end users), and even though it doesn't mesh with the idea that BW could do this work on its own, we have to work with what we have.
    We have come up with 3 possible ways to save this data to flat files and hopefully someone can tell us which might be the most effective.
    1.) Information Broadcasting. If we broadcast XML files to the portal, will the portals team be able to read that data into a SQL database? Is there another way to use broadcasting to create and send a flat file to specific location?
    2.) RSCRM_BAPI. This transaction seems to not support texts.
    3.) Open Hub. In order to get the open hub to work, we first have to save all of our query results to direct write data store objects using APD. (calculations based on current date, for example, would require daily full loads to the underlying data providers otherwise.)
    What is the best way to accomplish this? Is information broadcasting capable of doing this?
    Thanks!

    Hi Adam,
    Do you have to use flat files to load the information to a SQL database? (if so maybe someone else has a suggestion on which way would be the best).
    I try to stay away from flat file uploads as there is a lot of manual work involved. May I suggest setting up a connection to your SQL table in the DBCON table and then writing a small abap program to push data into the SQL database (which you can automate).
    1) Use APD to push data into a table within BW.
    2) Go to transaction SM30 > table DBCON and setup a new entry specifying the conncection parameters to your SQL database.
    3) In SE38 Write an ABAP program along the folloing lines (assuming the connection you set in DBCON is named conn1:
    data: con_name like dbcon-con_name
    con_name = 'conn1'.
    exec sql.
      set connection :con_name
    endexec.
    ****have a select statement which reads data from your table which the data is saved from the APD into an internal table**********
    ****loop on the internal table and have a SQL insert statement to insert the records into the SQL table as below******
    exec sql.
    insert into <SQL TABLE> (column names seperated by ,)
    values (column names of the internal table seperated by ,)  if the internal table is named itab the columns have to be specified as :itab-column1
    If you decide to take this approach you may find more info on DBCON and the process in sdn. Good Luck!
    endexec.

  • HELP!! - Need PL/SQL to write to a flat file!!

    I'm trying to query information about a customer's salesrep, and append the results to a flat file. I'm a beginner, and the following pseudocode is the best I have so far. Any advice would be much appreciated.
    Thanks in advance!!
    Paul
    CREATE OR REPLACE PROCEDURE paul IS
    file_handle utl_file.file_type;
    mgrname CHAR;
    mgrphone CHAR;
    mgrext CHAR;
    BEGIN
    utl_file.open('C:\WINNT\Profiles\pking\Desktop\outputfile.txt','w');
    SELECT
    name
    ,attribute7
    ,attribute8
    INTO
    mgrname
    ,mgrphone
    ,mgrext
    FROM
    ra_salesreps_all
    rem WHERE
    rem X-X-X-X-X
    rem
    rem EXCEPTION
    rem WHEN no_data_found THEN
    rem NULL;
    utl_file.putf(file_handle, mgrname, mgrphone, mgrext);
    utl_file.fclose(file_handle);
    END paul;
    null

    Below is a simple one....
    Procedure WRITE2FILE
    id_h in integer,
    matter in varchar2 default null
    IS
    v_FileHandle utl_file.file_type;
    root_dir varchar2(200);
    file_h varchar2(100);
    BEGIN
    file_h := 'msg_'&#0124; &#0124;id_h&#0124; &#0124;'.txt'; -- you can give dynamic file name
    root_dir := 'unix_or_nt/home/file_dir';
    v_FileHandle := utl_file.fopen(root_dir,file_h,'w');
    if matter is not null then
    utl_file.put_line(v_FileHandle,'Additional Information');
    utl_file.put_line(v_FileHandle,'------------------------------------------------------------------');
    utl_file.put(v_FileHandle,matter);
    utl_file.new_line(v_FileHandle,1);
    utl_file.put_line(v_FileHandle,'------------------------------------------------------------------');
    else
    utl_file.put(v_FileHandle,matter);
    end if;
    utl_file.fflush(v_FileHandle);
    utl_file.fclose_all();
    exception
    when utl_file.invalid_path then
    DBMS_OUTPUT.PUT_LINE('Invalid path:');
    when utl_file.invalid_mode then
    DBMS_OUTPUT.PUT_LINE('invalid_mode');
    when utl_file.invalid_filehandle then
    DBMS_OUTPUT.PUT_LINE('invalid_filehandle');
    when utl_file.invalid_operation then
    DBMS_OUTPUT.PUT_LINE('Invalid_operation. ');
    DBMS_OUTPUT.PUT_LINE('The File is not available.');
    when utl_file.read_error then
    DBMS_OUTPUT.PUT_LINE('read_error');
    when utl_file.write_error then
    DBMS_OUTPUT.PUT_LINE('write_error');
    when utl_file.internal_error then
    DBMS_OUTPUT.PUT_LINE('internal_error');
    when others then
    DBMS_OUTPUT.PUT_LINE(4, 'A problem was encountered while writing the document.');
    end ;
    Calling procedure>>>>>>>>>
    execute write2file(100,'Prints the matter in here.');
    will result in a file with name msg_100.txt and the contents of the file will be...
    Additional Information
    Prints the matter in here.
    1)Make sure the directory has write permissions
    2)Initialization parameter UTL_FILE = 'unix_or_nt/home/file_dir' on database server. If not, then put this in init.ora (ask your DBA) and restart the db.
    3)check the syntax for the '/' and '\' depending on your OS
    null

  • Problem Loading Microsoft Sql Serer table data to flat file

    Hi Experts,
    i am trying to load data from SQL Server table to flat file but its errror out.
    I have selected Staging area different form Targert ( I am using SQL Server as my staging area)
    knowlegde modue used: IKM SQL to file Append
    I reciee the following errror
    ODI-1217: Session table to file (124001) fails with return code 7000.
    ODI-1226: Step table to file fails after 1 attempt(s).
    ODI-1240: Flow table to file fails while performing a Integration operation. This flow loads target table test.
    ODI-1227: Task table to file (Integration) fails on the source MICROSOFT_SQL_SERVER connection POS_XSTORE.
    Caused By: java.sql.SQLException: Could not read heading rows from file
         at com.sunopsis.jdbc.driver.file.FileResultSet.<init>(FileResultSet.java:164)
    Please help!!!
    Thanks,
    Prateek

    Have you defined the File Datastore correctly with appropriate delimiter and file properties.
    Although the example is for oracle to file , see if this helps you in any way - http://odiexperts.com/oracle-to-flat-file.

  • SQL Query to get the # of files in folders in a library

    Anyone has the SQL query to list the number of files in each folders in a SharePoint library?

    Technically it's a grey area of support as MS don't mind you doing it but won't help you if you encounter problems, the script is much better though.
    Scratch that, the script isn't that good. It's using .items which won't work on large lists and is slow. The version below is untested but should be significantly faster and work on large lists.
    $starttime = Get-Date
    #Creating new site object
    $siteurl = Read-Host "Enter the name of your site and press enter"
    $site = New-Object Microsoft.SharePoint.SPSite($siteurl)
    #Assigning all webs (sites) to $webs
    $webs = $site.Allwebs
    #System Libraries defined so they won't be touched##
    $systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates",
    "Images", "List Template Gallery", "Master Page Gallery", "Pages",
    "Reporting Templates", "Site Assets", "Site Collection Documents",
    "Site Collection Images", "Site Pages", "Solution Gallery",
    "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub")
    Write-Host "Total number of webs that will be traversed: " $webs.count
    $DocLibsCount = 0
    $DocLibwItems = 0
    $totalitems = 0
    $subfolderitems = 0
    foreach($web in $webs)
    $listcoll = $web.lists
    foreach($list in $listcoll)
    if($list -eq $null)
    Write-Host
    else
    $base = $list.GetType()
    if($base.name -eq "SPDocumentLibrary")
    if ($systemlibs -contains $list)
    { continue}
    else
    $DocLibsCount += 1
    $totalitems += $List.itemcount
    $name = $list.Title
    $folders = $web.GetFolder($name).SubFolders
    for($etr = 0;$etr -lt $folders.count; $etr++)
    if($folders[$etr].Name -ne "Forms")
    Write-Host "Processing SubFolder ItemCount" -ForegroundColor Red
    $tempcount = $folders[$etr].ItemCount
    $subfolderitems += $tempcount
    Write-Host
    Write-Host
    Write-Host "Total # of Document Libraries: " $DocLibsCount -ForegroundColor Green
    Write-Host "Total # of Document Libraries that contain items: " $DocLibwItems -ForegroundColor Green
    Write-Host "Total # of items: " $totalitems -ForegroundColor Green
    Write-Host "Total # of items in DocLib\Subfolders: " $subfolderitems -ForegroundColor Green
    $finishtime = Get-Date
    Write-Host
    Write-Host “Script Duration” –ForegroundColor Yellow
    Write-Host “Started: “ $starttime –ForegroundColor Yellow
    Write-Host “Finished: “ $finishtime –ForegroundColor Yellow
    Stop-SPAssignment -Global

  • How to Save SQL query template as a text file

    Hi,
    How to save a SQL query template in my server as a Text file?? I tried using Text saver but it is not getting saved in the path specified.
    Sweety

    Hi Sweety,
    In the transaction editor (for business logic), add your query template to a transaction.  Add a Tracer after and link the Query property of the SQL Query action block to the Message property of the transaction.  Run the transaction and you will see your Action Block's sql script as the message from the Tracer.  You can do the same with the Server property, SD and ED, etc.
    Also if you wish to dynamically load the sql script into the query, you can do that at run time by linking a script source to the query property.  Useful to have if you have to use a complex expression to dynamically create your script.  This sometimes occurs when you have multiple filtering inputs from a webpage and you can reduce the number of tables in a complex join when the fields are left empty.  Normally this would be done as a way to improve the performance of the query.
    Hopefully this is clear.  If not I can probably provide some sample expressions.
    Regards,
    Mike

  • SQL query to find access database files?

    Odd request, but does anyone have a SQL query to find files? Specifically, I am looking for machines that have access databases on them. .mdb & .accdb

    Have you enabled inventory for them?
    Garth Jones | My blogs: Enhansoft and
    Old Blog site | Twitter:
    @GarthMJ
    i was trying to figure out where that option was but couldn't find it under hierarchy settings. perhaps it's been changed since 2012 R2? can you help point it out?

  • SQL*Loader - How to combine Flat File 3 columns and put into one single column

    Receive a flat file delimited by comma. Want to combine Flat File last 3 columns and put into one single column(Table).
    e.g.
    Flat File
    100,239,30,20,30
    While inserting into table want to combine last 3 columns and insert into invoice number column.
    302030

    It is not possible to combine the last 3 columns as those columns are seperated by commas and in the SQL Loader control file you must ve specified COMMA as an delimiter. So u better have all the columns in the table plus add one more column which holds the concatenation of the 3 columns.
    Vijay

  • How to dump a flat file into database?

    hi guys,
    I am new to jdbc. I am trying to transfer some data from flat file to database. What I can think of now is to create an array that is big enough to hold the entire file, dump the content of file in it and then store the array of data into database using insert statement. But this seems to be memory consuming especially when the file is very large. So could anyone tell me a more efficient way of doing that? thanks a lot.

    hi guys,
    I am new to jdbc. I am trying to transfer some data from flat file to database. What I can think of now is to create an array that is big enough to hold the entire file, dump the content of file in it and then store the array of data into database using insert statement. But this seems to be memory consuming especially when the file is very large. So could anyone tell me a more efficient way of doing that? thanks a lot.

  • Sql query o/p into text file using 11g

    i am using 11g. i want to write the result of the query into text file.how to do it

    Hi if you are using SQL*plus then try with SPOOL command or if your are using TOAD then you can export the result of the query into a file of your choice. example Excel cheet or text file.
    just right click on the result in your toad window and there will be a option of export result to a file.

  • Tricky SQL query... how to get all data in a single query?

    create table employee_definition (def_id number, def_name varchar(50));
    insert into employee_definition values (100, 'EMAIL');
    insert into employee_definition values (200, 'MOBILE_PHONE');
    insert into employee_definition values (300, 'HOME_PHONE');
    SQL> select * from employee_definition;
        DEF_ID DEF_NAME
           100 EMAIL
           200 MOBILE_PHONE
           300 HOME_PHONE
    create table employee_data (def_id number, def_value varchar(20), emp_id number);
    insert into employee_data values (100, '[email protected]', 123);
    insert into employee_data values (200, '01232222', 123);
    insert into employee_data values (300, '5555', 123);
    insert into employee_data values (100, '[email protected]', 666);
    insert into employee_data values (200, '888', 666);
    insert into employee_data values (300, '999', 666);
    insert into employee_data values (300, '444', 777);
    SQL> select * from employee_data;
        DEF_ID DEF_VALUE                EMP_ID
           100 [email protected]              123
           200 01232222                    123
           300 5555                        123
           100 [email protected]              666
           200 888                         666
           300 999                         666
           300 999                         777
    7 rows selected.I'm supposed to create a SQL that will return me the email, mobile_phone, and home_phone for a set of employees. The result will be something like this:
    EMPLOYEE ID | HOME_PHONE | MOBILE_PHONE | EMAIL
    123         |  5555  |    01232222      | [email protected]
    666         |  999  |    888      | [email protected]
    777         |  444  |    null     | nullThe thing I'm finding difficulty here is that the same column is used to store different values, based on the value in employee_definition table (something like a key/value pair). If I do:
    SQL> select emp_id, def_value as email from employee_data, employee_definition
      2  where employee_data.def_id = employee_definition.def_id
      3  and employee_definition.def_name = 'EMAIL';
        EMP_ID EMAIL
           123 [email protected]
           666 [email protected]'s partially ok.. I'm just getting the definition for 'EMAIL'. But how can I get all the values in a single query, knowing that the column stores different values based on def_name?

    Oh no, not again.
    Entity attribute models always seem like a great idea to people who have been in the profession for five minutes and lack any kind of fundamental knowledge.
    It staggers me that someone with 2,345 posts still believes "you need a 'detail table' for [storing multiple telephone numbers]"
    "A person can have multiple telephone numbers" is not an excuse to build a tired person_attribute table. Niether is the bizarre proposal by someone with over 4k posts who should know better in an earlier post that EAV models are necessary to support temporal fidelity.
    Taken to it's logical conclusion, EAV modelling leads to just two application tables. THINGS and THING_ATTRIBUTES. And when you consider that a THING_ATTRIBUTE is also a THING, why not roll those two tables up into one also? Hmmm, what does THINGS and THING_ATTRIBUTES look like? I know, TABLES and COLUMNS. Who would've guessed? SQL already provides the completely flexible extensible attribute model the advocates of EAV proscribe. But it also has data types, physical data independence, constraints and an efficient query language which EAV does not.
    EAV modelling errodes the semantics of the attributes which are bundled into the "attribute" table.
    There is no point in storing 12 different phone numbers with implied functional dependency to unconstrained and often repeating notional attributes like "MOBILE", "LANDLINE", "WORK", err, "WORK2", err, "MOBILE2", err, ... when this phone type attribute has no semantic value. When you want to call someone, you invariably want to retrive the prefered_phone_number which may depend on a time of day, or a call context.
    These things need to be modelled properly (i.e normalised to BCNF) within the context of the database.

  • Query filter selection list flat file

    I know that when using a "multiple single value" variable a user can paste or upload a text file of values BUT...
    is it possible to use a text file to create a query FILTER value list?
    (I mean in the query designer not at query run-time or in a workbook)
    thanks

    oh i just now see that with BI 7.0 you can right click on the right side of a filter selection and choose "Upload Selection"

  • Tricky SQL Query.. help

    Hi all,
    I am new with this forum, will be here often. i have a question about sq query that i need to run here.
    OK. the table
    customer
    CID_ID CREATE_DT
    0000000029003242 10-JUN-12
    0000000029003074 10-JUN-12
    0000000029003191 10-JUN-12
    0000000029002097 09-JUN-12
    0000000029004443 12-JUN-12
    0000000028975367 10-JUN-12
    0000000029004178 11-JUN-12
    0000000028998641 07-JUN-12
    0000000029003191 10-JUN-12
    0000000028998641 07-JUN-12
    10 rows selected
    the question :
    ok, for the result i wanted 2 column which is [cid_id, count(cid_id)] and it must be ordered by create_dt. i know it need group by and order by. i'm ok with using group by or order by them self, but i couldn't get these running at the same time beautifully.
    anybody please help me, i have workied on this for 3 days already still i cannot see how i can do this.

    /* Formatted on 8/16/2012 2:51:53 PM (QP5 v5.139.911.3011) */
    WITH tmp
            AS (SELECT '0000000029003242' cid_id, '10-JUN-12' create_dt FROM DUAL
                UNION ALL
                SELECT '0000000029003074', '10-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000029003191', '10-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000029002097', '09-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000029004443', '12-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000028975367', '10-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000029004178', '11-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000028998641', '07-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000029003191', '10-JUN-12' FROM DUAL
                UNION ALL
                SELECT '0000000028998641', '07-JUN-12' FROM DUAL)
    SELECT cid_id, create_dt, cnt
      FROM (SELECT cid_id,
                   create_dt,
                   COUNT (
                      cid_id)
                   OVER (PARTITION BY cid_id
                         ORDER BY TO_DATE (create_dt, 'dd-Mon-yy'))
                      cnt,
                   ROW_NUMBER ()
                   OVER (PARTITION BY cid_id
                         ORDER BY TO_DATE (create_dt, 'dd-Mon-yy'))
                      rn
              FROM tmp)
    WHERE rn = 10000000028975367     10-JUN-12     1
    0000000028998641     07-JUN-12     2
    0000000029002097     09-JUN-12     1
    0000000029003074     10-JUN-12     1
    0000000029003191     10-JUN-12     2
    0000000029003242     10-JUN-12     1
    0000000029004178     11-JUN-12     1
    0000000029004443     12-JUN-12     1
    Edited by: Indra Budiantho on Aug 16, 2012 12:53 AM

  • SQL Server Source and Flat File Target in OWB

    Hello All,
    I have a question, is it possible to have SQL Server as Source and Flat File as Target without using any intermediate Oracle table(s).
    like
    SQL Server --> ETL Operators --> Flat file?
    as i m getting errors in data type conversions here. but if i replace flat file with Oracle table it works fine. (though i have used some conversion functions for data types as well in Flat file loading but it is giving me errors)
    Thanks in advance.
    Tayyeb

    Yes this should be fine. Are you using the gateway to access SQLServer or code template mappings? You need to track which columns are throwing the data type conversion errors.
    Cheers
    David

Maybe you are looking for

  • Oracle db problem in Global Creation

    Hi all, We are experiencing problems at the start of a process itself..It worked well in the development and the QA environment but when it went into production it gave an error...at the start of Global Creation activity.. We suspect this error to be

  • Solaris 10 X86 no internet connexion

    Hi, Just after installation, Mozilla 1.7 cannot access internet : The interface e1000g0 works (ping, scp, ssh in both directions) Is it a configuration problem ? Help should be appreciated. # mozilla -debug IP ADDRESS: e1000g0 = xx.xx.49.12 DNS DOMAI

  • Search results in wwlog_activity_log1$

    We are using Portal Release 1 (3.0.9.8.4). The portal30 activity monitoring table WWLOG_ACTIVITY_LOG1$ records search activity, including the string searched for. It doesn't appear to record whether the search returned any rows, and if so how many. D

  • How to modify the OIDDAS Create User page

    I am using Portal 902 and want users to register themselves to my Portal. I've have enabled Self-Registration but the pages need much cosmetic re-work for my purposes. I've discovered how to replace the default db generated SSO Login page with a cust

  • Importing album folders problem

    Hi. Why when I drag or import an album folder already stored on a hard drive into iTunes. iTunes then lists each track of that album as individual albums