script to fetch oracle database table in csv format - oracle

I am using this script as below
set colsep ','
set heading on
set headsep on
set pagesize 0
set trimspool off
spool C:\DXC\books11.csv
Select * from test_extract;
spool off
exit
but the problem with this is
ARKO ,1A , 20
ARKO1 ,1B , 20
space is comming after the values of each attribute as per the lenght of the attribute.
required output :
ARKO,1A,20
ARKO1,1B,20

As far as I can tell, no SET command will help.
One option - that helps - is to name all columns you're spooling and concatenate them using desired column separator.
For example, this is what you currently have:
SQL> set colsep ','
SQL> set heading on
SQL> set headsep on
SQL> set pagesize 0
SQL> set trimspool off
SQL> select * From dept;
10,ACCOUNTING ,Zagreb
20,RESEARCH ,DALLAS
30,SALES ,CHICAGO
40,OPERATIONS ,BOSTON
But, if you do it as follows:
SQL> select deptno ||','|| dname ||','|| loc from dept;
10,ACCOUNTING,Zagreb
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
it looks as you wanted. Drawback? You'll have to type all those columns.

Use SQLcl, it's in your SQL Developer bin directory, works like SQLPlus, only better.
It's SQL.exe on Windows for example, but also available as it's own 25mb download on Oracle.com.
set sqlformat csv
spool file.csv
select * from table;
It'll give you exactly what you're asking for.

Related

How do I export oracle query result without enclosure

I have oracle 12c and trying to export query result to csv or text file but I dont want any enclosure of my data. I have tried SET SQLFORMAT csv which creates csv file but data comes in double quotes then I tried SET SQLFORMAT delimited | but that also comes with double quoted. I also tried SET MARKUP csv on delimeter | quote off it also gave me same result. I dont think MARKUP command works on 12c but it did not give me error. Here is my script:
SET SQLFORMAT delimited | ;
spool 'C:\Temp\MyResults.csv';
select 1 AS Col1, 'Data Line 1' AS Col2 from dual UNION select 2 AS Col1, 'Data Line 2' AS Col2 from dual;
spool off;
This gives me result:
"COL1"|"COL2"
1|"Data Line 1"
2|"Data Line 2"
But I want without double quotes on string data.
COL1|COL2
1|Data Line 1
2|Data Line 2
I would appreciate if someone can give me any poption.
Thanks.
I have the following working with Oracle 19 (client and server on Linux):
SQL> set markup CSV on quote off
SQL> desc t;
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> select * from t where rownum=1;
OBJECT_ID,OBJECT_NAME
16,TS$
SQL> set markup csv on quote on
SQL> select * from t where rownum=1;
"OBJECT_ID","OBJECT_NAME"
16,"TS$"
SQL>

SQL Loader issue loading decimal negative numbers

I have the following issue using SQL Loader to load a small table, my process populates a field which can be populated for possitive or negative decimal numbers, and the problem is that when the number is possitive, the process rounds with 2 positions, but if it's negative only use 1 position. I need 2 position, and this is the simple code:
UNRECOVERABLE LOAD DATA
TRUNCATE
INTO TABLE [SCHEMA].TABLE1
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
FIELD1 "trim(:FIELD1 )"
)
Example:
Source: 12345,78 Target: 1234,78
Source: -12345,78 Target: -1234,8
Edit1: It just happend with number with 7 or more integer length (X < -1000000), but I can insert this numbers with regular insert instead of SQLLoader.
Edit2: I have noticed that it is not a problem with sql*loader because the .ctl is already wrong. This file is generated with python and SQL with this properties:
set colsep |
set headsep off
set pagesize 0
set trimspool on
set linesize 10000
set termout off
set feedback off
set arraysize 250
But I dont know where it is defined the lenght of each field, and this looks the error.
I'd say that you're not looking correctly. Data is, probably, stored OK, but you don't see it. Here's an example:
SQL> create table test (field1 number);
Table created.
SQL>
Control file:
load data
infile *
replace into table test
fields terminated by '|' trailing nullcols
(
field1
)
begindata
12345,78
-12345,78
987654321
-987654321
987654321,01234
-987654321,01234
111333444,887766
-112333444,887766
Loading session & the result:
SQL> $sqlldr scott/tiger#orcl control=test26.ctl log=test26.log
SQL*Loader: Release 11.2.0.2.0 - Production on Uto Kol 21 08:52:10 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 7
Commit point reached - logical record count 8
SQL> select * from test;
FIELD1
----------
12345,78
-12345,78
987654321
-987654321
987654321
-987654321
111333445
-112333445
8 rows selected.
SQL>
Ooops! Seems to be wrong, eh? But, if you fix the column format ...
SQL> col field1 format 999g999g999g990d000000
SQL> select * from test;
FIELD1
-----------------------
12.345,780000
-12.345,780000
987.654.321,000000
-987.654.321,000000
987.654.321,012340
-987.654.321,012340
111.333.444,887766
-112.333.444,887766
8 rows selected.
SQL>
See? Everything is here.
I guess that, if you used a GUI, you wouldn't have such problems. The former sentence doesn't mean that you should NOT use SQL*Plus - on the contrary; just ... learn how to use it properly :)
[EDIT, after reading the comment]
It is SET NUMFORMAT you might be looking for:
SQL> select -12334.789123 from dual;
-12334.789123
-------------
-12334,789
SQL> set numformat 999g999g990d000000
SQL> select -12334.789123 from dual;
-12334.789123
-------------------
-12.334,789123
SQL>

Multiple columns added to single column when spooling table data to csv

I'm writing a shell script to spool multiple columns in PL/SQL table to CSV. But all the columns are added to a single column in the CSV file. Can't seem to figure out what the problem is.
FILE="x26837a/test.csv"i
sqlplus -s MyConnection << EOF
set heading on;
set pagesize 1000;
set tab on;
column owner format a10;
column parent_object_name format a20;
column sub_object_name format a30;
column object_type format a40;
column invalid_abbr format a50;
column email_flag format 9999999;
set linesize 300;
SPOOL $FILE;
SELECT * FROM NM_STD_TBL WHERE ROWNUM < 10;
SPOOL OFF;
EXIT;
EOF
I believe, if you are using SQL Plus, you will need to do
set colsep ,
so that it creates a CSV for you.

When or Why to use a "SET DEFINE OFF" in Oracle Database

I'm watching a Script in Oracle and I see something I don't recognize
REM INSERTING into database1."Users"
SET DEFINE OFF;
Insert into database1."Users" ("id","right") values ('1','R');
I'm looking for documentation about "set define off" and it's literally writing "disable the parsing of commands to replace substitution variable with their values"
I don't really understand what they want to say.
Can anyone help me?
By default, SQL Plus treats '&' as a special character that begins a substitution string. This can cause problems when running scripts that happen to include '&' for other reasons:
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
Enter value for spencers:
old 1: insert into customers (customer_name) values ('Marks & Spencers Ltd')
new 1: insert into customers (customer_name) values ('Marks Ltd')
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks Ltd
If you know your script includes (or may include) data containing '&' characters, and you do not want the substitution behaviour as above, then use set define off to switch off the behaviour while running the script:
SQL> set define off
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks & Spencers Ltd
You might want to add set define on at the end of the script to restore the default behaviour.
Here is the example:
SQL> set define off;
SQL> select * from dual where dummy='&var';
no rows selected
SQL> set define on
SQL> /
Enter value for var: X
old 1: select * from dual where dummy='&var'
new 1: select * from dual where dummy='X'
D
-
X
With set define off, it took a row with &var value, prompted a user to enter a value for it and replaced &var with the entered value (in this case, X).

Why does the result in TOAD and SQLPlus differ?

I have this query.
select
dbms_metadata.get_ddl('USER', username) || '/' usercreate
from
dba_users where username = 'NSAGUN';
In TOAD, I get this text. (using SAVE AS TAB DELIMITED)
USERCREATE
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT TABLESPACE "PIN01"
TEMPORARY TABLESPACE "PINTEMP"
/
But in SQLPlus I only get this:
USERCREATE
--------------------------------------------------------------------------------
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT T
Why is that? And how can I make the output in SQLPlus the same as in TOAD?
Try using these settings in SQL*Plus before executing the query:
set long 1000000
set longchunk 1000000
set linesize 200
The dbms_metadata.get_ddl function returns a CLOB value and by default SQL*Plus sets the LONG variable to 80 bytes.
SQL> set long 1000000
SQL> set pagesize 0
SQL> SELECT
2 dbms_metadata.get_ddl('USER', 'LALIT') || '/' usercreate
3 from
4 dba_users where username = 'LALIT'
5 /
CREATE USER "LALIT" IDENTIFIED BY VALUES 'S:F10EA8C6778ACE16430E4714FE8C41CFB
2C9E5BC73ADDC503E134EA91AF9;H:076ADC10B6F6540DEEB030DF6C97A752;C6F71E6F6BA0F4BD'
DEFAULT TABLESPACE "USERS"
TEMPORARY TABLESPACE "TEMP"/
SQL>
LONG {80|n}
Set the maximum width (in chars) for displaying and copying LONG values.
SET PAGES[IZE] {14 | n}
Sets the number of rows on each page of output in iSQL*Plus, and the
number of lines on each page of output in command-line and Windows
GUI. You can set PAGESIZE to zero to suppress all headings, page
breaks, titles, the initial blank line, and other formatting
information.

Resources