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>
Related
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;
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).
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.
I try to save the output of an Oracle SELECT command into a bash variable.
I tried the following lines but it didn't work really well...
ACCESS_SQL=`{
sqlplus << EOF
${USER}/${PASSWORD}#DB
set head off;
set feedback off;
set pagesize 5000;
set linesize 30000;
set serveroutput on;
DECLARE
data varchar(5000);
BEGIN
select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='${USER_ID}';
dbms_output.put_line(data);
END;
/
exit;
EOF
}`
The error statement I get is :
SQL> SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7 select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='PZ230';
*
ERROR at line 4:
ORA-06550: line 4, column 110:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 4, column 2:
PL/SQL: SQL Statement ignored
I was wondering if using a varchar is the right thing to do...
You don't need to select into a variable and then use dbms_output.put_line to print it out. (Your select into statement won't work anyway, because you can't select multiple columns into a single data variable.)
Instead, do it like this:
data=$(sqlplus -S ${USER}/${PASSWORD} << EOF
set head off
set feedback off
set pagesize 5000
set linesize 30000
select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG from uam.access_list where USER_ID='${USER_ID}';
exit
EOF)
echo "$data"
When i'm using FML999G999G999G999G990D00 as my Number Format Mask in Oracle Apex, it shows Value as $800.00. I need to Replace $ with another Currency Sign.
How Could i do this ?
You need to modify the session parameter NLS_CURRENCY to change the currency:
SQL> ALTER SESSION SET NLS_CURRENCY='EUR';
Session altered.
SQL> SELECT to_char(800, 'FML999G999G999G999G990D00') FROM dual;
TO_CHAR(800,'FML999G999G999G999G9
---------------------------------
EUR800,00
Or with DBMS_SESSION:
SQL> BEGIN dbms_session.set_nls('NLS_CURRENCY', 'GBP'); END;
2 /
PL/SQL procedure successfully completed.
SQL> SELECT to_char(800, 'FML999G999G999G999G990D00') FROM dual;
TO_CHAR(800,'FML999G999G999G999G9
---------------------------------
GBP800,00
You can also specify the currency directly with TO_CHAR:
SQL> SELECT to_char(800, 'FML999G990D00', 'NLS_CURRENCY=''£''') FROM dual;
TO_CHAR(800,'FML999G9
---------------------
£800,00