sqlplus remove \r \n \t from spool - oracle

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?

Related

whitespace trimming in the right side of my report issue

i am using sqlplus to generate report,i will spool the output of query in sqlplus screen and making report
now i want a report which having spaces
SELECT '123'||lapd(count(1),12,'0')||rpad(' ',10',' ') from dual;
i expect the report should spool as (im showing space as dot(.) to explain you)
123000000000009..........
but i got
123000000000009
i have lot of query like this...
i am using
set trimspool on
set termout off
set linespace 300
any idea?
SET TRIMSPOOL ON removes trailing blanks at the end of each displayed or spooled line.
If that's so, and you want to keep those blanks at the end of the result, why on Earth did you use that setting? Remove it!

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;

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

Is there any way to make the output in SQLPlus wider(on Windows )?

In an attempt to more easily view the output in SQLPlus, I fiddled with window-size properties and font(tiniest font).
But this is what I get :
It is exactly the same, just much less readable.
I just want to run the query :
select * from user_tables where rownum < 10 ;
Without the messy unreadable lines (currently I can't tell apart the data from table headings)
With the help of one useful SQL*Plus option and one additional helper option it is possible. Put
set linesize 32767
set trimout on
before your select.
Enjoy.

SQLPlus export to CSV (output format problem)

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.

Resources