Spool query result to file

Hi everyone,
I would like to spool the results of my query to a CSV file with a delimiter ';' on a hard drive. I use Toad for Oracle, my script worked fine previously on version 9, and now I have version 10.1, and it doesn't work anymore.
What happens is I do get the output alright, but for some reason the output creates blank spaces at the end of each line, but only where the last column value for the line is 0.
For example, I get something like this:
Let's say I have an output of 5 columns, first 3 being string and the 4th and 5th being numbers
A B C D E
stringA stringB stringC 1.01 26.2
stringA stringB stringC 10.002 0
stringA stringB stringC 23 1001.1001
In CSV this looks like this:
A;B;C;D;E
stringA;stringB;stringC;1.01;26.2
stringA;stringB;stringC;10.002;0
stringA;stringB;stringC;23;1001.1001
The first and third row would appear fine, but the second row (the one that ends with a zero) would have a bunch of blank spaces after the zero.
Here is the script that I use:
SET PAGESIZE 0 LINESIZE 2000 TRIMSPOOL ON FEEDBACK OFF ECHO OFF TERMOUT OFF;
SPOOL "C:\SPOOL_RESULT.CSV"
SELECT
'A'||';'||
'B'||';'||
'C'||';'||
'D'||';'||
'E'
FROM DUAL
UNION ALL
SELECT
A||';'||
B||';'||
C||';'||
D||';'||
E
FROM SOME_TABLE;
SPOOL OFF;
CLEAR BUFFER;
My opinion is that this TRIMSPOOL doesn't work as effectively as it should. I've tried several others, such as TRIMOUT, but nothing seems to work.
I could bypass this by setting a 'dummy' column at the end that would always have some value <> 0, say 1, but this is not a clean solution.
Any ideas?
Thanks

Hi,
Try this one.
SET PAGESIZE 0 LINESIZE 2000 TRIMSPOOL ON FEEDBACK OFF ECHO OFF TERMOUT OFF;
SPOOL "C:\SPOOL_RESULT.CSV"
SELECT
'A'||';'||
'B'||';'||
'C'||';'||
'D'||';'||
'E'
FROM DUAL
UNION ALL
SELECT
A||';'||
B||';'||
C||';'||
D||';'||
trim(E)
FROM SOME_TABLE;
SPOOL OFF;
CLEAR BUFFER;Trim will remove all the blank spaces.
SQL> select length('0      ') from dual;
LENGTH('0')
          7
SQL> select length(trim('0      ')) from dual;
LENGTH(TRIM('0'))
                1

Similar Messages

  • Storing query results in file server via Information Broadcasting service

    Dear Experts,
    We use BI7.0.
    I want to get the query results as PDF file or Excel file
    stored in file server or BW server, EP server by night batch job.
    (Not the distribution of query via E-mail.)
    BW3.5, I think we can use pre-calculation service for that case.
    BW7.0, I could not find such settings in EP Broadcasting service.
    Please help me...
    Kanako Imamura

    Thanks Deepu!
    I found another tips;
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/b891da90-0201-0010-fba7-8c35824c83a8
    Kanako Imamura

  • CohQL command line Tool select query output in file (unix)

    Ho to insert Coherence CohQL command line Tool select query result in file in unix platform. I have tried below query but failed
    query.cmd -s -c -l "select * from 'dist-example'" >myOutput.txt
    I also tried below query, result is displayed in command line but no file is created in folder.
    select * from dist-example >myOutput.txt
    I also tried one more query, result is displayed in command line but no file is created in folder.
    select * from dist-example >/opt/bin/myOutput.txt

    Hi,
    In Unix you should use query.sh instead of query.cmd
    In your case then the command would be smething like:
    query.sh -s -c -l "select * from 'dist-example'" >myOutput.txt
    You can also try using '>>' instead of '>' in order to redirect to a text file.
    Hope this helps.
    -Cris

  • Downloading query results in background

    Hi -- I have a simple query that I would like to run on a daily basis to download information for a 3rd party system. Two questions:]
    (1) I would like the output file to automatically overwrite the previous day's file. When I run the query in dialog mode, I am notified that the file already exists and provided with the option to overwrite it. But when I run the query in background mode, it doesn't overwrite the file. Does anyone know how I can force it to overwrite? I should note that I am using the 'File store' option under Output format.
    (2) Every line in my output file has an extraneous '0' at the end of the line. In fact, it's like a whole extra output column, always populated with '0'. Does anyone know how I can get rid of this? It's happening if I specify the file as a .xls or as a .txt file.
    Thanks for any guidance!!

    Hi,
    You cannot download files to a pc in the background. There are many posts concerning this topic. Use the Search button for more information on this.
    Without seeing your table structure it's a bit hit and miss, but I would say you may have an extra column in your table.
    You should use the 'private file' option with enhancement SQUE0001 to dynamically store query results to files in the background.
    Reward if helpfull...
    Cheers,
    Sruti.

  • How to output a query results into a text file

    How to output a query results into a text file instead of outputing it to the screen..
    is there a way for us to write a SQL query which specifies to output the query results to a text file.
    Pls let me know how to do it
    Thanking u in advance
    regards
    Muraly

    Muraly,
    If you are using SQL*Plus 8.1.6 or later, you can also spool output to a file in HTML format, eg
    SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON
    SPOOL c:\temp\report.html
    SELECT DEPARTMENT_NAME, CITY
    FROM EMP_DETAILS_VIEW
    WHERE SALARY>12000;
    SPOOL OFF
    SET MARKUP HTML ENTMAP OFF
    In iSQL*Plus 9.0.1 (the browser-based interface to SQL*Plus) onwards, you can also send the HTML output to a new web browser window, or an html file -- much easier than the command line method.
    Alison

  • How to get query result in comma dilimited text or excel file?

    Does anybody know how to get query results in comma delimited
    text file or excel file, I tried spool abc.txt, but the result
    showed some ------ lines
    Thanks

    Try doing this in your sql scripts
    set heading off
    set pagesize 0
    set linesize 4000
    set feedback off
    set verify off
    set trimespace on
    set colsep ","
    spool output.txt
    select * from dual (or whatever you are querying
    spool off
    There may be a couple other set statement that you could add but
    this should get you started in the right direction

  • Export query result to txt file

    Hello,
    I'm trying to export a query result to txt file but I facing some problems.
    I'm using the comand below:
    set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
    set colsep ,
    spool C:\estados.txt
    select id_estado,cod_estado,nme_estado from tb_estado;
    spool off
    First problem: My select statement is being writen on my estados.txt
    Second problem: The results are being writen with a lot of blank spaces in start of line, for example, instead of write "1,AC,Acre" the line is write as " 1,AC,Acre".
    Third problem: The "spool off" statement is being written on my estados.txt
    How could I solve it?
    Thanks

    1. Works here - what version of SQL*PLUS are you using?
    2. If you want to prevent spaces, you have to write your SELECT like this:
    select id_estado || ',' || cod_estado || ',' || nme_estado from tb_estado;Note: not tested.
    3. See 1.
    C.

  • Query results to a file

    I have a requirement to put the query result in a file(.csv),
    Pls let me know is there a possibility to achieve this using dbms_output package
    Thanks

    859486 wrote:
    I have a requirement to put the query result in a file(.csv),
    Pls let me know is there a possibility to achieve this using dbms_output packageNo. The DBMS_OUTPUT package is almost ALWAYS the wrong package to use. It is the most misunderstood and most frequently abused piece of PL/SQL software. So no - it is not suited in anyway to spool CSV output. In fact, trying it with a large SQLdataset can crash the Oracle server - as memory will be exhausted and the swap daemons will trash CPU utilisation.
    To read or write a physical o/s file - the typical package used is UTL_FILE. It is fairly easy using UTL_FILE to create a CSV file.
    However, there could be far better alternatives. The CSV file may be needed by a web client - in which case the file can be created as a CLOB and provided as a Mime text/csv data stream to a web browser (which usually will automatically open in Excel).
    The CSV file may need to be delivered via e-mail. In which case, the CSV file contents can be delivered via UTL_SMTP to a mail server as an e-mail attachment.
    The CSV file may be needed to be delivered to a remote server. In which case the CSV file can be created as a CLOB and this CLOB can be ftp'ed via PL/SQL to the remote server as a text file.
    So there are many options in creating and delivering a CSV file. Using DBMS_OUTPUT though is not a sensible option - ever.

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

  • How to generate XML file from oracle database query result

    Hi dudes,
    as stated on the subject, can anyone suggests me how can i achieve the task stated above??
    Here is a brief description of my problem:
    I need to create a XML file once i query from the oracle database, and the query result returned from the database will be stored in XML file.
    I'd searched around the JAXB, DOM, SAXP and the like basic concepts, but i still don't know how to start??
    Any suggestions ???

    Read this:
    http://www.cafeconleche.org/books/xmljava/chapters/ch08s05.html
    You might have to read more of the book to understand that chapter.

  • Export query results to flat file with dynamic filename

    Hi
    Can anybody can point me how to dynamic export query serults set to for example txt file using process flows in OWB.
    Let say I have simple select query
    select * from table1 where daterange >= sysdate -1 and daterange < sysdate
    so query results will be different every day because daterange will be different. Also I would like to name txt file dynamicly as well
    eg. results_20090601.txt, results_20090602.txt, results_20090603.txt
    I cant see any activity in process editor to enter custom sql statment, like it is in MSSQL 2000 or 2005
    thanks in advance

    You can call existing procedures from a process flow the procedure can create the filename with whatever name you desire. OWB maps with file as target can also create a file with a dynamic name defined by an expression (see here ).
    Cheers
    David

  • How to save query result on the file server as excel file?

    Hi everyone,
    I need to save the the query result on the file server as excel file.
    Do you have any idees how can I do it?
    Regards
    Erwin

    i dont think you can save as excel file but you can save it as CSV use APD or RSCRM BAPI for this

  • How to upload the web query result  to CSV/Text file

    Hi,
    Kindly help me on the following.
    1)       I have info-provider (IP), which has info-objects "company code" and "supplier".
    2)       I built a query by putting company-code with a variable in free-charateristics and supplier-code in row.
    3)       When I run this query, it will ask company-code as input and the result will be filtered using company-code.
    4)       For this query, I created a web-template and assigned data-provider to display the data on the web.
    5)       The user wants to enter input company-code='1000' and write the data in a CSV file in a specific directory. i.e, by clicking the execute button (after entering company code) , the result should go to the CSV or text file. i.e. the application should automatically generate the file with query results.
    Note: The user should not use context menu to EXPORT TO CSV OR EXCEL file.
    Please let me know, if there is any tutorial. Thanks a lot advance help.
    Regards
    Kandasamy

    1. SELECT INTO
    Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
    Use AdventureWorks2008R2;
    Go
    ---Insert data using SELECT INTO
    SELECT AddressLine1, City
    INTO BothellAddresses
    FROM Person.Address
    where City = 'Bothell';
    GO
    ---VERIFY DATA
    Select AddressLine1, City
    FROM BothellAddresses
    ---DROP TABLE
    DROP TABLE BothellAddresses
    GO
    2. INSERT INTO SELECT
    Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
    another table.
    Use AdventureWorks2008R2;
    Go
    ---Create Table
    CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
    ---Insert into above table using SELECT
    INSERT INTO BothellAddresses(AddressLine1, City)
    SELECT AddressLine1, City
    FROM Person.Address
    where City = 'Bothell';
    ---VERIFY DATA
    Select AddressLine1, City
    FROM BothellAddresses
    ---DROP TABLE
    DROP TABLE BothellAddresses
    GO
    Regards,
    Vishal Patel
    Blog: http://vspatel.co.uk
    Site: http://lehrity.com

  • Export query results into .csv file?

    Hello I have a T-SQL script that gets row counts for a specified date range and then needs to loop (by incrementing +1 day to get the next day's counts) for a large date range.  I'm aiming to output & append each query results day counts
    into a .csv file via a SQL Agent job since this will take quite a while to complete.
    Would using the following as an example template...
    INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
    SELECT Field1, Field2, Field3
    FROM DatabaseName
    ...be the method or something else?
    If this is good to use I've tried running this but get the following error:
    Msg 7357, Level 16, State 2, Line 76
    Cannot process the object "SELECT * FROM [FileName.csv]". The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" indicates that either the object has no columns or the current user does not have permissions
    on that object.
    Thanks in advance.

    Hi Techresearch7777777,
    The error in your post says that the file FileName.csv has to be created with the column names in the first row. Like:
    Field1,Field2,Field3
    Either you can create a schema.ini file under the same folder:
     [FileName.csv]
     Format=CSVDelimited
     ColNameHeader=False
     Col1=Field1 [DataType]
     Col2=Field2 [DataType]
     Col3=Field3 [DataType]
    For the [DataType],you can reference
    Schema.ini File (Text File Driver)
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • Query result to ASCII file output

    Seems like there SHOULD be some rediculously simple solution
    to this - but I sure haven't found it yet! I have a query which
    produces the a varying number of columns (based on a multi-select
    cfselect) which I need to output to a comma delimited ASCII (.TXT
    or .DAT or .CSV file). I have all the steps worked out when the
    number of columns and their names are known, but I need to make
    this variable. First choice would be to be able to just "DUMP" the
    query result into a text file. CFDUMP can send results to a text
    file, but NOT in comma delimited format, and with all kinds of info
    besides just the basic data. (NOT good!) Second choice, since the
    "append to text file" uses an output property that expects a comma
    delimited string variable would be to use CFLOOP to loop thru the
    rows in the query and put them (a row at a time) into a variable,
    then use that variable for the file append. Problem is I can't find
    a way to just say "send *ALL* to the string variable. Every example
    I have been able to find includes specifying the column NAMES when
    doing anything with a row in the query result!!! Short of a brute
    force series of if/then statements that specify EACH POSSIBILITY
    (in this case 60 - 75 of them, and the number WILL GROW!) I haven't
    been able to find a solution. There's GOTTA BE A WAY!!!!! :)

    auntiealias
    > I tried that, but as far as I could figure you still
    have to have a specific column name
    Look at the udf more closely. If you read the documentation
    it states all of the parameters are optional, except the query. So
    if you do not supply column names, the function will use the
    columnList variable to determine the names of the columns in the
    query. If you look at the actual code, notice it uses array
    notation to extract the values (not evaluate) .
    ... (code to create query) ...
    <!--- pass in the query object only ---->
    <cfset test = QueryToCSV2( data )>
    http://cflib.org/index.cfm?event=page.udfbyid&udfid=1197
    > I have a cfselect with multiple set to "yes" and the
    selections are "which columns of
    > data do you want to see?"
    Hopefully you are not using the raw form value directly in
    your sql, because that is a sql injection attack just waiting to
    happen.

Maybe you are looking for