I am running the following code in SQL developer (oracle) to run the query and export a csv file into a folder. I would like to add the system date to the filename as well. I am using the following code. Although it does the job, it asks the user to input the date in the pop up window. I am looking to get rid of the pop up window and rather have the code use the system date instead. Is there any way i can eliminate the user input window?
Second issue is that this also brings in the SQL code to the output file along with the query results, is there any way to avoid bringing in the SQL as well?
set VERIFY off
set FEEDBACK off
set echo off
set heading off
col date_stp new_value date_stp
Select to_char(sysdate,'yyyymmdd') date_stp from dual;
Spool 'I:\Folder\ExportData&date_stp..csv';
SET sqlformat csv;
Select Customer, ID, etc -- the content of the query
Spool off;
Here's how: in order to avoid displaying the select statement within the spooled file, you need to
set term off
set feed off
but not directly in SQL*Plus (or SQL Developer); you'll have to save the following contents into a .SQL file and run it using #.
So: p.sql
set term off
set feed off
col sd new_value x
select to_char(sysdate,'YYYYMMDD') sd from dual;
spool dept&x..txt
select * From dept;
spool off
Testing:
SQL> #p --> call the .sql file
SQL> $dir *.txt
Volume in drive C is OSDisk
Volume Serial Number is 7635-F892
Directory of C:\Users\lf
12.06.2020. 21:13 494 dept20200612.txt --> file name is OK
1 File(s) 494 bytes
0 Dir(s) 260.040.732.672 bytes free
SQL> $type dept20200612.txt
--> no SELECT statement here
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
In order to stop SQL*Plus or SQL Developer asking for value of a substitution variable (that's what &something represents), run set define off.
Related
I am trying to make a cron job that runs once a day. On invoking the shell script, it calls a Sql file. Which in turn Spools data into a file. This file is Picked by Shell Script and then mailed accordingly. Problem is when I try to spool data , it is writing elapsed time instead of writing actual result of query to the output file.
Here is the sql file I am using.
set define off
set numformat 99999999999999999999999999
set markup html on
set serveroutput on
set head on
set pages 3000
set echo off
DECLARE
total integer :=0;
total = select count(*) from t_c_table1 vt, t_c_table2 ti WHERE vt.f_item_id = ti.f_item_id (+) AND (f_update_date < sysdate - 30)order by F_INSERT_DATE desc;
IF total > 0 then
spool /home/output.csv
select f_name, count (*) from t_c_table1 where F_INSERT_DATE < sysdate-100 group by f_item_provider_id;
spool off
END IF
I get output like Elapsed: 00:00:00.506 in the spooled csv file.
Where am i going wrong?
Please Help.
Thanks in Advance..
Code you posted is wrong, it won't even compile in Oracle so I'm surprised that you got anything at all.
As there's no SET TIMING ON, I'm not sure what produced the elapsed time line in the spooled file. Maybe it is some old, previously created CSV file you're looking at?
Apart from the fact that SPOOL is a SQL*Plus command (so you can't invoke it in a PL/SQL procedure), the way you calculated the TOTAL variable's value is wrong - it should be part of the SELECT ... INTO statement.
SQL> declare
2 total integer := 0;
3 begin
4 select count(*)
5 into total
6 from dept;
7
8 if total > 0 then
9 spool test.csv
10 select * from dept;
11 spool off;
12 end if;
13 end;
14 /
spool test.csv
*
ERROR at line 9:
ORA-06550: line 9, column 12:
PLS-00103: Encountered the symbol "TEST" when expecting one of the following:
:= . ( # % ;
SQL>
If you want to spool data conditionally, you'll have to use the UTL_FILE package.
Or, you could do it "interactively" so that SQL*Plus asks you whether you want to spool data or not, as Alex Poole described in his answer here.
I want to execute query from batch file and put exact output of query into csv file.
Like:
my query is : select sysdate from dual.
Output of this :
Sysdate
23-feb-2013
So, in csv it stored as
sysdate
22-feb-2013
DB: Oracle 11g
OS: windows 7
Kindly plz help me to achieve this ..
c:\myfolder\mybatch.bat:
call sqlplus oracleuser/oraclepwd#oracledb #c:\myfolder\myquery.sql
c:\myfolder\myquery.sql:
set head off
set feed off
set pagesize 0
spool c:\myfolder\myresult.csv
prompt sysdate
select to_char(sysdate, 'dd-mon-yyyy') from dual;
spool off
set head on
set feed 6
quit
I run a query using sqlplus command line interface. The query will fetch some 30 million records. I need to export the result to either csv or xls format. Can anyone let me know if this is possible?
Any help is much appreciated.
Thanks in advance.
Try spool myresults.csv before your select statement, which Excel can easily open.
EDIT
Like this:
SET UNDERLINE OFF
SET COLSEP ','
--That's the separator used by excel later to parse the data to columns
SET LINES 100 PAGES 100
SET FEEDBACK off
--If you don't want column headings in CSV file
SET HEADING off
Spool ~\myresults.csv
--Now the actual query
SELECT * FROM YOUR_TABLE;
Spool OFF
EDIT 2
You might want to batch your results if you're going to query 30M records. I've never gone that far in an excel file but the limit is 65535 rows (that would be 458 files for 30M records).
I'd go with cutting up your query into block of 60K blocks and spooling each select to a different excel file, maybe by looping on an integer and concatenating it to the end of each filename.
SET PAGESIZE 50000
SET FEEDBACK OFF
SET MARKUP HTML ON SPOOL ON
SET NUM 24
SPOOL sample.xls
SELECT * from users;
SPOOL OFF
SET MARKUP HTML OFF SPOOL OFF
This option will help you to export directly to a excel file
SET PAGESIZE 40000
SET FEEDBACK OFF
SET MARKUP HTML ON
SET NUM 24
SPOOL file_name.xls
---- Execute your query
SPOOL OFF
SET MARKUP HTML OFF
SPOOL OFF
Spool sqlplus to xls format
How can i add column name on top of the column in sql generated xls ouput file.. I am getting all the data but without column heading .. I am doing this in c shell sql script.. I have my SET HEADINGS ON but still i am not getting my heading.. do i have to do anything else in the sql script to populate the heading...plz help
`SET PAGESIZE 0
SET LINESIZE 1000
SET LONG 100000
SET FEEDBACK OFF
SET ECHO OFF
SET HEADING ON
SET SPACE 1
SET FLUSH OFF
SET TRIMSPOOL ON
SET NUMFORMAT 9999999999.90
SELECT USER_ID
From ...
Where...;`
please share us your query. What client tool do you use?
Asuming you use sql*plus
set heading on
set pages 10000
select dummy user_id
from dual;
It's the SET PAGESIZE 0 that is causing the normal heading to be suppressed.
SET PAGESIZE
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.
Increase PAGESIZE to avoid printing
headings frequently, or set it to 0 to
prevent headings being displayed.
So one option is to set pagesize to a very large value (50,000 is the largest allowable value in my version of SQLPlus).
If your query is going to produce more rows than that, you will have heading appearing in the middle of the data. So another option is to keep headings off and add a separate statement to your script to output a header row. You could use SELECT 'USER_ID FROM DUALorPROMPT USER_ID` to output the text "USER_ID".
SELECT COLUMN_NAME might be what you're looking for...
I'm running some queries in PL/SQL Developer, and one of the columns in the result has 18-digit numbers. Instead of displaying the entire number in the resulting grid, PL/SQL Developer displays only 15 digits in scientific notation.
I tried to find a way to change this in the preferences of the program, so that I'll see the entire number, just like set numwidth does in SQL*Plus. But my search was futile.
How do I change this setting?
Turns out this is possible!!!
Tools -> Preferences -> SQL Window ->
Number fields to_char
Use to_char, then you get the all the numbers:
select to_char ( t.reference_nr), t.reference_nr from rss_ing_cc_imp t
1 95209140353000001009592 9,5209140353E22
2 25546980354901372045601 2,55469803549014E22
3 75203220356000583867347 7,52032203560006E22
4 25546980357904327000017 2,55469803579043E22
5 95209140358000000700337 9,5209140358E22
6 95209140359000000596387 9,5209140359E22
7 25546980361131086003511 2,55469803611311E22
8 25546980361901390031808 2,55469803619014E22
9 85207130362051881964326 8,52071303620519E22
10 95209140363000000634885 9,5209140363E22
11 25546980364131099000436 2,55469803641311E22
12 95209141001000001006196 9,5209141001E22
13 85207131001100892094030 8,52071310011009E22
14 75203221001000590476576 7,52032210010006E22
You can also set the column format(Using the same table name as above...)
column reference_nr format 99999999999999999999999999999999
Select reference_nr from rss_ing_cc_imp;
REFERENCE_NR
95209140353000001009592
25546980354901372045601
Or ( new session ) which probably is better:
show numwidth
numwidth 10
Select reference_nr from rss_ing_cc_imp;
REFERENCE_NR
9.5E+22
2.6E+22
Set numwidth 30
show numwidth
numwidth 30
Select reference_nr from rss_ing_cc_imp;
REFERENCE_NR
95209140353000001009592
25546980354901372045601
SET sqlformat ansiconsole;
This will set the output format for any queries that you run hereafter. There are other sql formats but this is probably the best for your situation.
To revert to what you had earlier, use.
SET sqlformat;
*This has been verified on SQLDeveloper Version 18.3.0.277, Build 277.2354
Same answer as Ilya Kogan, but in PL SQL Dev 13 the Preferences has moved and is now under an little tuner icon in the title bar. Then SQL Window -> Number fields to_char