create new directory in spool - oracle

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.

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;

Is there any PL/SQL script to modify column value of each table in each schema of Oracle Database

I have written pl/sql code to modify column values in a table of one schema. But I need to execute the same script in each table of each schema. I will take the list of the table name, column name from spreadsheet.
define schema_name = 'D';
define hours_offset = '(-3/24)';
set echo off;
set timi off;
set heading off;
set feedback off;
set linesize 250;
spool convert_01.sql
select 'set echo on;' from dual;
select 'set timi on;' from dual;
select 'set heading on;' from dual;
select 'set feedback on;' from dual;
select 'set linesize 250;' from dual;
select 'spool convert_01.log;' from dual;
select 'update '||owner||'.'||table_name||' set '||column_name||'
='||column_name||' + &hours_offset;'
from dba_tab_columns where owner = '&schema_name' and
(
data_type = 'DATE' or
data_type like 'TIMESTAMP%'
) and
data_type not like 'TIMESTAMP%WITH TIME ZONE'
order by owner, table_name, column_name;
select 'spool off;' from dual;
spool off;
I need to execute the same script in each table of each schema
That's most probably not true. There are users (schemas) you shouldn't modify at all, e.g. SYS, SYSTEM, CTXSYS, APEX180200 and such.
Therefore, you'd rather collect list of users you want to skip (or the ones you want to affect; it's up to you). That list would then be used instead of '&schema_name'.
A simple option is to store list of users into a table and use its contents as a subquery in your current script.
Unrelated, but:
I have written pl/sql code ...
Nope; what you wrote is a pure SQL script readable by SQL*Plus (and, possibly, some other tools which understand commands you used).

SQL developer query results to dynamic csv file

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

sql syntax is missing & error handling

I want to handle errors in sql script, I have put some statements for it.But its not working. I think i am missing begin & end statements position.
here is my sql script please correct it.
set colsep ',';
set trimout off;
set pagesize 0;
set trimspool off;
set feedback off;
set heading off;
set heading off;
set verify off;
set errorlogging on;
var envame varchar2(20)
exec :envame := '&1'
set errorlogging on;
spool C:\UsersDesktop\batch\pres64.csv app
begin
select '&&1', user_name, user_id from employee where designation = 'manager';
exception
when others then
dbms_output.put_line('ERROR');
end;
exit;
this gives output as
12
13
14
15
.
.
please suggest modifications..
spool C:\UsersDesktop\batch\pres64.csv app
begin
select '&&1', user_name, user_id from employee where designation = 'manager';
exception when others then
dbms_output.put_line('ERROR');
You have mixed SQL*Plus with PL/SQL exception handling. SPOOL is a SQL*Plus command. It won't work if you use the BEGIN-END PL/SQL block.
Since you are using SQL*Plus, I would strongly recommend to use the new SQL*Plus error logging feature rather doing it in PL/SQL. It was introduced in release 11.1.
I have written an article about it here http://lalitkumarb.wordpress.com/2014/01/21/sqlplus-error-logging-new-feature-release-11-1/
For example,
SQL> set errorlogging on
SQL> show errorlogging
errorlogging is ON TABLE LALIT.SPERRORLOG
SQL> desc sperrorlog
Name Null? Type
----------------------------------------- -------- -----------------------
USERNAME VARCHAR2(256)
TIMESTAMP TIMESTAMP(6)
SCRIPT CLOB
IDENTIFIER VARCHAR2(256)
MESSAGE CLOB
STATEMENT CLOB
SQL> selct * from dual;
SP2-0734: unknown command beginning "selct * fr..." - rest of line ignored.
SQL>
So, the above SP2 error is now logged in the sperrorlog table.
SQL> select timestamp, username, statement, message from sperrorlog;
TIMESTAMP USERNAME STATEMENT MESSAGE
------------------------------ -------- -------------------- --------------------------------------------------
06-APR-15 10.42.49.000000 AM LALIT selct * from dual; SP2-0734: unknown command beginning "selct * fr...
" - rest of line ignored.
SQL>

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;

Resources