SQL developer query results to dynamic csv file - oracle

I am trying to export my query results to csv file with spool command in SQL Developer. It works when csv file name is static, but now I need to create file name dynamically and I tried as follow but obviously its not working. Can anyone please help?
var CatCode char(5) ;
exec :CatCode := 'ZK';
exec :csvFile := 'c:\temp\MyCSV_' || trim(:CatCode) || '.csv';
set feedback off;
SET SQLFORMAT csv;
spool csvFile
SELECT * FROM Products WHERE CategoryCode = :CatCode;
spool off;
SET SQLFORMAT;
set feedback on;
I tried spool command with spool :csvFile or spool &csvFile but did not help.

VAR CatCode CHAR ( 5 );
EXEC :CatCode := 'ZK';
SET FEEDBACK OFF;
SET SQLFORMAT csv;
SET TERMOUT OFF
column filename new_value filename
SELECT 'c:\temp\MyCSV_' || trim(:CatCode) || '.csv' as filename FROM DUAL;
SPOOL &filename
SELECT * FROM Products WHERE CategoryCode = :CatCode;
SPOOL OFF;
SET SQLFORMAT;
SET FEEDBACK ON;
Also you might want to check this askTOM article.
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:3581757800346555562

Related

Adding extra data/information to filename using spool in sqlplus

I'm trying to achieve the below requirements in the filename being exported as csv
FILENAME: filename_yyyymmdd_nnnnnn_xxxxxx.csv
where
yyyymmdd = Datestamp on when the Account file was generated
nnnnnn = 6-byte random generated batch ID
xxxxxx = 6-byte zero-padded string that contains the number of records in the file
So far, I have the below query saved as a script which I run on sqlplus.
set head off;
set feedback off;
set term off;
set pagesize 0;
set linesize 3000;
set trimspool on;
set colsep ,;
set verify off;
set echo off;
ALTER SESSION SET NLS_DATE_FORMAT= 'MM-DD-YYYY';
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
column date_stamp new_value sys_date noprint
column rnd_num new_value random noprint
column row_count new_value rc noprint
select to_char(sysdate,'mmddYYYY') date_stamp
from dual;
select lpad(round(dbms_random.value(1,999999)),6,'0') rnd_num from dual;
/* select count(*) as row_count
from table a, table b join on a.id = b=id where condition; */
spool filepath.&sys_date..&random..&rc..&1..csv
select statement fetching the actual data;
spool off;
exit;
The problem is:
I am able to generate the random no,but not able to add record count (xxxxxx part of the filename requirement). When the part commented is uncommented and run, the sql script runs but no file is generated. When it is run with the "select count(*)" part as commented, the file is generated as expected, has the random no bit(nnnnnn) but doesn't have the no of records(xxxxxx), obviously because it is commented out. How to include the no of records in the filename as well?
So, with enough reading around the web, I found the solution. The row_count for some reason in my query was returning with a 5 empty spaces padded value then the actual row_count. Posting the answer below:
set head off;
set feedback off;
set term off;
set pagesize 0;
set linesize 3000;
set trimspool on;
set colsep ,;
set verify off;
set echo off;
ALTER SESSION SET NLS_DATE_FORMAT= 'MM-DD-YYYY';
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
column date_stamp new_value sys_date noprint
column rnd_num new_value random noprint
column row_count new_value rc noprint
select to_char(sysdate,'mmddYYYY') date_stamp
from dual;
select lpad(round(dbms_random.value(1,999999)),6,'0') rnd_num from dual;
select row_count from (select trim(count(*)) as row_count
from table a ...;
spool filepath.&sys_date..&random..&rc..&1..csv
select statement fetching the actual data;
spool off;
exit;

How to do dynamic spool's using sqlplus by batch-file

I'm trying save multiples txt archives with querye result using a single .sql archive called by a batch-file for automate-work.
My Batch-file:
set d=%location%result.txt
set f=%location%result2.txt
echo exit | sqlplus GOVMANPATCHORACLE/GOVMANPATCHORACLE#%SERVER%/%SERVICE% #C:\Users\enrique.erbs\Desktop\JOB\Tools\extrator_parametros\queryoracle.sql '%d%' '%f%'
*'%d%' contains the save locale to spool
My archive .sql (queryoracle.sql):
SET PAGESIZE 50000
SET LINESIZE 800
SET VERIFY OFF
SET ECHO ON
SET HEAD ON
SET SERVEROUTPUT ON;
SET PAGES 0
SET DEFINE ON;
COLUMN COD_PARAMETRO FORMAT a100 HEADING COD_PARAMETRO;
COLUMN DES_PARAMETRO FORMAT a100 HEADING DES_PARAMETRO;
COLUMN DES_VALOR FORMAT a100 HEADING DES_VALOR;
DECLARE
myvar1 varchar2(30);
myvar2 varchar2(30);
BEGIN
myvar1 := '$1';
myvar2 := '$2';
SPOOL 'myvar1'
SELECT TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO AS COD_PARAMETRO, TDFE_PARAMETRO.DES_PARAMETRO AS DES_PARAMETRO, TDFE_PARAMETRO_APLICACAO.DES_VALOR AS DES_VALOR FROM TDFE_PARAMETRO_APLICACAO INNER JOIN TDFE_PARAMETRO ON(TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO=TDFE_PARAMETRO.COD_PARAMETRO);
SPOOL OFF
SPOOL 'myvar2'
SELECT * FROM TDFE_PARAMETRO_APLICACAO;
SPOOL OFF
END;
I tryed some options and the most closest to get the result what i want is using ">> %location%\result.txt" at end of sqlplus call.
Somebody can help me ?
You were not far at all. Actually, When executing SQLPLUS and passing him a file to execute and arguments, It will replace every single occurence of &1 By your first Argument, Every occurence of &2 by your second argument, etc ...
This should work :
SET PAGESIZE 50000
SET LINESIZE 800
SET VERIFY OFF
SET ECHO ON
SET HEAD ON
SET SERVEROUTPUT ON;
SET PAGES 0
SET DEFINE ON;
COLUMN COD_PARAMETRO FORMAT a100 HEADING COD_PARAMETRO;
COLUMN DES_PARAMETRO FORMAT a100 HEADING DES_PARAMETRO;
COLUMN DES_VALOR FORMAT a100 HEADING DES_VALOR;
SPOOL '&1'
SELECT TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO AS COD_PARAMETRO, TDFE_PARAMETRO.DES_PARAMETRO AS DES_PARAMETRO, TDFE_PARAMETRO_APLICACAO.DES_VALOR AS DES_VALOR FROM TDFE_PARAMETRO_APLICACAO INNER JOIN TDFE_PARAMETRO ON(TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO=TDFE_PARAMETRO.COD_PARAMETRO);
SPOOL OFF
SPOOL '&2'
SELECT * FROM TDFE_PARAMETRO_APLICACAO;
SPOOL OFF
For reference : https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve044.htm#SQPUG127

create new directory in spool

I am trying to create a script to spool in a dynamically create a new folder
here is my code
set feedback off;
set pages 0;
set term off;
column dt new_value _dt;
column mn new_value _mn;
select 'C:\Users\rjen01\Desktop\'||to_char(sysdate,'MON')||'\' from dual;
select to_char(sysdate,'ddMONyyyy_hh24mi')||'.csv' dt from dual;
spool &_mn &_dt;
select sysdate from dual;
spool off;
and it gives the error
Cannot create SPOOL file C:\Users\rjen01\Desktop\MAR\ 21MAR2017_1227.csv
its because there is no folder C:\Users\rjen01\Desktop\MAR.So how can i create a new folder dynamically in spool command.
host - Executes a host operating system command without leaving SQL*Plus.
I've change your example and now it's working.
set feedback off;
set pages 0;
set term off;
column dt new_value _dt;
column mn new_value _mn;
select to_char(sysdate,'MON') mn from dual;
host mkdir &_mn
select to_char(sysdate,'MON')||'\'||to_char(sysdate,'ddMONyyyy_hh24mi')||'.csv' dt from dual;
spool &_dt;
select sysdate from dual;
spool off;
/
I don't know if this solution will be work in sqldeveloper.

Column Values shifting to next row while creating a CSV using SQL *Plus

I want the output of a SQL query in a csv file. While other such jobs are running successfully, here is a job, one of them, which gives me a csv like this. I have checked the loader control file - it seems fine. I have also checked the SQL query - also, perfectly fine. What could be the problem?
SPOOL CSV_TO_BE_GENERATED.csv;
SET LINESIZE 1000;
SET PAGESIZE 0;
SET TRIMSPOOL ON;
SET FEEDBACK OFF;
SET SQLBLANKLINES ON;
WHENEVER SQLERROR EXIT SQL.SQLCODE
select
'COLUMN_1'||','||
'COLUMN_2'||','||
'COLUMN_3'||','||
'COLUMN_4'||','||
'COLUMN_5'||','||
'COLUMN_6'||','||
'COLUMN_7'||','||
'COLUMN_8'
from DUAL;
select COLUMN_1||','||
COLUMN_2||','||
COLUMN_3||','||
COLUMN_4||','||
COLUMN_5||','||
COLUMN_5||','||
COLUMN_7||','||
COLUMN_8
from some_table ;
SPOOL OFF
EXIT;

remove spaces or tab when spooling

I am trying to extract data(spooling) from oracle and it got spaces or tab between the data.
My script is like this.
set head off;
set feed off;
set lines 32767;
set trimspool on;
set trimout on;
set pages 0;
set space 0;
set tab off;
set wrap off;
set colsep "|";
spool FNAME;
select select id, name, job from table;
spool off;
exit
The data should be like this:
ID|NAME |JOB
Suppose ID and NAME should not have any spaces or tab between it
but instead, i have data like this:
ID<--spaces/tab-->|NAME |JOB
There's additional spaces or tab added between ID and NAME. i already set the parameters. I dont know what to change.
SQL*Plus is formatting the results into aligned columns for you. You can avoid this by doing the formatting yourself:
select select id || '|' || name || '|' || job from table;

Resources