SQLPlus export to CSV (output format problem) - oracle

I'm facing an issue with an interface script, supposed to export the content of some table of an ORACLE database into CSV file, which is then followed by an import of those CSV into a MYSQL database.
STEP1: SQLPlus export to CSV
set headsep off
set heading off
set term off
set echo off
SET RECSEPCHAR \n
set pagesize 0
set linesize 0
trimspool on
SET FEEDBACK OFF
spool as_ex_feature.csv
select '"AS'||'"|"'||feature_group||'"|"'||feature_desc||'"|"
||feature_order||'"|"'||prod_code||'"'
from MYVIEW WHERE MYCONDITIONS;
spool off;
-> this step is generating the CSV file, but the format seems incorrect, as I can find some carriage return in the output.
Also you'll see in STEP2 that we define an "ENCLOSED BY" value how could I get that one included in the export format (doesn't seem to be the case right now).
STEP 2: MYSQL load
LOAD DATA INFILE 'mycsvfile' REPLACE INTO TABLE `mt_feature`
FIELDS TERMINATED BY '|'
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
This script had to be rebuilt for some technical reasons and the Mysql part had not been changed and is working fine with a proper CSV file to import.
The issue seem to be coming from that SQLPlus export, where I need to admit I don't have much knowledge on. Maybe I should use another method to get those files generated?
Please let me know if you need additional details, I feel blind...
Script running on oracle 10g, Linux, Mysql 4.x
Thanks!

SET LINESIZE 0 isn't valid, the value has to be between 1 and 32767. So I imagine it's wrapping the content at the default line length, which is 80 unless you've already got it set in a glogin script.

If you prefix any lines of code with (at least) four spaces in SO then it'll be formatted correctly, e.g.
select "AS'||'"|"'||
feature_group||'"|"'||
feature_desc||'"|"'||
feature_order||'"|"'||
prod_code||'"'
from MYVIEW
WHERE MYCONDITIONS;
Sounds like you may need to replace any embedded newline chars in the stored data....
SELECT "AS'||'"|"'||
TRANSLATE(feature_group, CHR(10), '\\n') ||'"|"'||
(etc).
And I'm not sure about setting the linesize to 0.

Related

SqlDeveloper SPOOL: Blank first line

I want to export multiple tables to a semicolon separated file each using SqlDeveloper (version is 4.1.5.21).
Currently I use
SET SQLFORMAT delimited
spool ..\dir\table1.csv
select * from table1;
spool off;
for each table in a script, which is fine.
The problem is that the resulting file has a blank first line. Is it somehow possible to delete it? I've done some research and it looks like the function which would take care of that in SqlPlus is not implemented in SqlDeveloper.
I've also tried to accomplish the same in SqlPlus, but I didn't even get close to the result which is produced by SqlDeveloper.
If it's not possible to get rid of that blank line using spool, is there any sql inbuilt function I could use to manipulate the resulting flag so I don't have to run a SqlDeveloper and then a bash script to get proper files?
If anyone stumbles upon this question:
As Alex pointed out in the comments, this is a bug in version 4.1.15 (and maybe earlier versions) of SqlDeveloper. To get rid of it, upgrade to 4.2.0.
If you are, like me, for some reason stuck with an earlier version, you can use sqlcl which is shipped together with the SqlDeveloper to execute your script without the described problems.
My .sql script looks like this:
SET ECHO OFF
SET FEEDBACK OFF
SET sqlformat delimited ; " "
spool ..\relative\path.csv
select * from table1;
SPOOL OFF;
QUIT;

Avoid double quotes around column while spooling to csv from oracle

I need to export a select query output to text file using spool. The output by default adds double quotes around the column. How to Prevent it?
The query returns only one column.
Ideally i need to remove headers as well as the double quotes from the output. below is the script file i use in oracle Developer. but heading is also not removed.
set echo off
SET HEADING OFF
SET PAGESIZE 0
SET COLSEP ''
spool 'D:\public\cvs_txPCG.txt'
select /*csv*/
pcg from temptx;
spool off;
output
"PCG"
"76259737020150320000504281565213310052440093515652109.2909.290101"
"19519905620160502000504283153419040044861008644759203.3903.390101"
"49424051620160220000504284594314590009220713032964404.3804.380202"
"88761197020151025000504284594315180036700812132964401.9901.990101"
You can't get rid of the double-quotes with the /*csv*/ directive. The slightly more convenient way of doing this now is with the sqlformat command, but even with sqlformat delimited you have to have the string enclosed.
Using CSV format for a single column seems rather pointless anyway though, unless you have delimiters in your string you want to escape. If you are really exporting that one column and don't have delimiters then just leave out the /*csv*/ directive. If you have multiple columns or strings that may contain delimiters then the enclosures are useful to stop those delimiters being misinterpreted when opening in Excel or some other external tool.
If you really want CSV format without enclosures you can either build up the output manually by concatenating the column(s) with comma literals, which still allows you to spool to a file:
select pcg ||','|| some_other_col ||','|| etc from ...
Or you can run the query using Run Statement (Ctrl-Enter) and then export the results from the data grid, from the context menu that appears when you right-click on the output. When you export and choose CSV or delimited format you can choose whether to include the header and which delimiter and enclosures to use - and you're allowed to choose 'none'. If you don't want to set that on each export you can set it as a default, from Tools->Preferences:
You can't do it with the /*csv*/ directive or sqlformat csv though.
I know it is too old but got the answer with my search today, simply use
set markup csv on quote off
select 1 id, 'Venkat' name from dual;
Output will be
ID,NAME
1,Venkat
Didn't even think of any other SET commands
If the double quotes are part of the data you can use the trim() function to remove them.
select /*csv*/
trim(both '"' from pcg) as pcg from temptx;
Oracle documentation for the trim function:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions199.htm

Why is SPOOL writing my output with LINESIZE set to 32767?

I am trying to spool certain data, but for some reason it gets wrapped to the next line.
This is the script I am using to SPOOL (I am adding the full script with comments in case it has any significance)
SET SERVEROUTPUT ON FORMAT WRAPPED SIZE UNLIMITED
SET LONG 2000000000
SET LINESIZE 32767
SET VERIFY OFF
SET FEEDBACK OFF
SET TERMOUT OFF
SET HEADING OFF
SET ECHO OFF
SET PAGESIZE 0
SET NEWPAGE NONE
SET TRIMSPOOL ON
/* -------------------------------------------------- COMMENT --------------------------------------------------
COMMENT1
COMMENT2
COMMENT3
COMMENT4
COMMENT5
*/
SPOOL "File_Name_MySYNONYM_SCRIPT-11111111.sql"
select SCRIPT FROM MY_TABLE_2_UPDATE where OBJECT_ID =11111;
SPOOL OFF
SCRIPT is a CLOB column and for the given ID contains:
CREATE OR REPLACE PUBLIC SYNONYM "MY_SYNONYM_12345" FOR "MS1"."MY_OBJECT1_SETUP";
This is the Output I get in the spooled file:
CREATE OR REPLACE PUBLIC SYNONYM "MY_SYNONYM_12345" FOR "MS1"."MY_OBJECT1_SETU
P";
You can see the P"; gets wrapped to the second line. The line is actually wrapped, it is not my editor (Notepad++) that is showing it wrong, I have "Word Wrap" deactivated.
I don't know what other SQL PLUS parameter I need to set to fix this issue.
Adding my comment as an answer as it seemed to help.
Try adding this after the SET command and before the SPOOL: "COLUMN SCRIPT FORM A3000". If sqlplus defaults the column SCRIPT to say 80 it will wrap.
However, reading further it seems you have other formatting issues, and also it seems the data is from DBMS_METADATA.GET_DLL.
I have written many tools (going back to v7) to extract DDL to scripts so that schemas can be rebuilt, and although DBMS_METADATA.GET_DDL is a huge help I still find it doesn't quite do the whole thing as neatly as you would like. So be warned. My scripts combine GET_DDL and some custom extract queries to get the nice clean usable scripts.
To remove extraneous line feeds, try this is your session:
exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',false);
Documentation for this is here

sqlplus command problems

I am a newbie to sqlplus. I am trying to write a simple script to extract data to a file.
First problem I am having: It describes the variables on the top of the output file, like
Old Value was
New Value is
I want this not be output if possible.
Secondly, i am trying to write the contents of a field to a file. The content of the field can vary, I have set the Linesize to 8000 but the return is around 50 characters or so. How do i get the full contents of the field?
Thank you in advance for your help.
To prevent SQL*Plus showing the old and new variable values you need to set verify off:
SQL> SET VERIFY OFF
To pipe the output to a file you need the spool command:
SQL>SPOOL C:\myfile.txt
SQL>SELECT sysdate
2 FROM dual;
SYSDATE
---------
30-NOV-11
SQL> SPOOL OFF
Hope it helps...

sqlplus remove \r \n \t from spool

Is there any sql*plus command to remove \r \n and\t from the result set that's going out to the spool file? That is, "trim" every record?
We've used set trim on in the past, but it doesn't seem to bue what we need right now. I'm trying to avoid calling oracle's translate, chr functions in the sql query.
For example,
set termout off
set spool somefile.dat
set lin 600
select data from mytable;
set spool off;
exit;
My query returns this
|DATA|
|\n \t\t\t\t\t thisistheactualdata \t\t\t\t\t\t\n|
And I'd like to keep this in my spooled file
thisistheactualdata
update
Well, we ended up doing something like this.
set tab off;
spool /home/oracle/out.dat
set linesize 20
set termout off
set trim on
select regexp_replace(l,'(\t|\n)','') from test;
spool off;
exit;
But got some bad news: We need to run this in oracle 8, and regexp_replace doesn't seem to be available. :(
Thanks in advance.
I don't think you're going to be able to do this with a SQL*Plus directive. SQL*Plus is a pretty thin client and isn't designed to touch the data from the result set itself.
If you don't want to use the built-in SQL functions to modify the output then I think you're stuck with post-processing a spooled file.
EDIT by DCookie:
Since I missed the OP's original request for a non-TRANSLATE/CHR based solution (I focused on the OP's lament that they were stuck with 8i), in fairness to dpbradley I'm going to withdraw my answer and include it as part of this one, since it was clearly considered. Here's what I wrote:
You might try the TRANSLATE function for your Oracle 8 situation:
SELECT TRANSLATE(L,'A'||CHR(10)||CHR(9)||CHR(13),'A') FROM test;
Without trying it, does
SET TAB OFF
have the desired effect?

Resources