Oracle Samples
Oracle Samples
http://www.plpdf.com/23-926.html
http://plnet.org/
http://orafaq.com/cgi-bin/sqlformat/pp/utilities/sqlformatter.tpl //gr8 site
_____________
begin
execute immediate 'garbage';
exception
when others then
dbms_output.put_line( 'error msg is'||sqlcode);
dbms_output.put_line( 'error code is '||sqlerrm );
dbms_output.put_line( 'new code is '||dbms_utility.format_error_backtrace );
dbms_output.put_line ( 'type of statement executed: '
|| dbms_sql.last_sql_function_code ( )
);
raise;
end;
/
--------
how can one search pl/sql code for a string/ key value?
select type, name, line
from user_source
where upper(text) like '%&keyword%';
__________
how can one search pl/sql code for a string/ key value?
select object_name,
to_char(created, 'dd-mon-rr hh24:mi') create_time,
to_char(last_ddl_time, 'dd-mon-rr hh24:mi') mod_time,
status
from user_objects
where last_ddl_time > '&check_from_date';
___________
set serveroutput on size 200000
-------
is there a limit on the size of a pl/sql block?
yes, the max size is not an explicit byte limit, but related to the parse tree
that is created when you compile the code. you can run the following select
statement to query the size of an existing package or procedure:
select * from dba_object_size where name = 'procedure_name';
----------
sql and pl/sql formatter site
http://orafaq.com/cgi-bin/sqlformat/pp/utilities/sqlformatter.tpl
----------
rem -----------------------------------------------------------------------
rem filename: encode.sql
rem purpose: demonstrate simple encoding and decoding of secret messages
rem this method can be extended to create simple password
rem encryption
rem date: 15-feb-2001
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
select translate(
'hello world', -- message to encode
'abcdefghijklmnopqrstuvwxyz ',
'1234567890!@#$%^&*()-=_+;,.') encoded_message
from dual
/
select translate(
'85@@%._%*@4', -- message to decode
'1234567890!@#$%^&*()-=_+;,.',
'abcdefghijklmnopqrstuvwxyz ') decoded_message
from dual
/
--------------------
rem -----------------------------------------------------------------------
rem filename: password.sql
rem purpose: simple password encryption package to demonstrate how
rem values can be encrypted and decrypted using oracle's
rem dbms obfuscation toolkit
rem note: connect to sys as sysdba and run ?/rdbms/admin/catobtk.sql
rem date: 18-mar-2003
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
dbms_obfuscation_toolkit.desencrypt(
input_string => v_data,
key_string => c_encrypt_key,
encrypted_string => v_encrypted_val);
return v_encrypted_val;
end encrypt;
end password;
/
show errors
-- test if it is working...
select password.encrypt('password1') from dual;
select password.decrypt(app_password.encrypt('password1')) from dual;
select password.encrypt('psw2') from dual;
select password.decrypt(app_password.encrypt('psw2')) from dual;
------------------
/*
------------------------------------------------------------------------------
filename: random.txt
purpose: random number/ string generator package
author: unknown
original: http://orafaq.net/scripts/sql/random.txt
edits:
19990908 phil rand <[email protected]> added functions rand_string(), smaller().
------------------------------------------------------------------------------
*/
begin
select userenv('sessionid')
into seed
from dual;
end random;
/
-- some examples:
select random.rand_max(10) from dual;
select random.rand_max(10) from dual;
select random.rand_string(20) from dual;
select random.rand_string(20) from dual;
------------
rem -----------------------------------------------------------------------
rem filename: leapyear.sql
rem purpose: check if a year is a leap year
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
-- let's test it
set serveroutput on
begin
if isleapyear(2004) then
dbms_output.put_line('yes, it is a leap year');
else
dbms_output.put_line('no, it is not a leap year');
end if;
end;
/
-----------
rem -----------------------------------------------------------------------
rem filename: ftpclient.sql
rem purpose: pl/sql ftp client
rem date: 19-nov-2003
rem author: russ johnson, braun consulting
rem -----------------------------------------------------------------------
* three functions are available for ftp - put, get, and ftp_multiple.
ftp_multiple takes
* a table of records that define the files to be transferred (filename,
directory, etc.).
* that table can have 1 record or multiple records. the put and get
functions are included
* for convenience to ftp one file at a time. put and get return true if the
file is transferred
* successfully and false if it fails. ftp_multiple returns true if no
batch-level errors occur
* (such as an invalid host, refused connection, or invalid login
information). it also takes the
* table of file records in and passes it back out. each record contains
individual error information.
*
* example
* --------------------
* transfer multiple files - 1 get and 2 put from a windows machine to a host
(assuming unix here).
* display any errors that occur.
* declare
*
* v_username varchar2(40) := 'rjohnson';
* v_password varchar2(40) := 'password';
* v_hostname varchar2(255) := 'ftp.oracle.com';
* v_error_message varchar2(1000);
* b_put boolean;
* t_files brnc_ftp_pkg.t_ftp_rec; -- declare our table of
file records
*
* begin
*
* t_files(1).localpath := 'd:\oracle\utl_file\outbound';
* t_files(1).filename := 'myfile1.txt';
* t_files(1).remotepath := '/home/oracle/text_files';
* t_files(1).transfer_mode := 'put';
*
* t_files(2).localpath := 'd:\oracle\utl_file\inbound';
* t_files(2).filename := 'incoming_file.xml';
* t_files(2).remotepath := '/home/oracle/xml_files';
* t_files(2).transfer_mode := 'get';
*
* t_files(3).localpath := 'd:\oracle\utl_file\outbound';
* t_files(3).filename := 'myfile2.txt';
* t_files(3).remotepath := '/home';
* t_files(3).transfer_mode := 'put';
*
* b_put := brnc_ftp_pkg.ftp_multiple(v_error_message,
* t_files,
* v_username,
* v_password,
* v_hostname);
* if b_put = true
* then
* for i in t_files.first..t_files.last
* loop
* if t_files.exists(i)
* then
* dbms_output.put_line(t_files(i).status||' | '||
* t_files(i).error_message||' | '||
* to_char(t_files(i).bytes_transmitted)||' | '||
* to_char(t_files(i).trans_start,'yyyy-mm-dd
hh:mi:ss')||' | '||
* to_char(t_files(i).trans_end,'yyyy-mm-dd
hh:mi:ss'));
* end if;
* end loop;
* else
* dbms_output.put_line(v_error_message);
* end if;
*
* exception
* when others
* then
* dbms_output.put_line(sqlerrm);
* end;
*
* credits
* --------------------
* the w3c's rfc 959 that describes the ftp process.
*
* http://www.w3c.org
*
* much of the pl/sql code in this package was based on java code written by
* bruce blackshaw of enterprise distributed technologies ltd. none of that
code
* was copied, but the objects and methods greatly helped my understanding of
the
* ftp client process.
*
* http://www.enterprisedt.com
*
* version history
* --------------------
* 1.0 11/19/2002 unit-tested single and multiple transfers between
disparate hosts.
*
*
*/
/**
* exceptions
*
*/
ctrl_exception exception;
data_exception exception;
/**
* constants - ftp valid response codes
*
*/
/**
* ftp file record datatype
*
* elements:
* localpath - full directory name in which the local file resides or will
reside
* windows: 'd:\oracle\utl_file'
* unix: '/home/oracle/utl_file'
* filename - filename and extension for the file to be received or sent
* changing the filename for the put or get is currently not allowed
* examples: 'myfile.dat' 'myfile20021119.xml'
* remotepath - full directory name in which the local file will be sent or
the
* remote file exists. should be in unix format regardless of ftp
server - '/one/two/three'
* filetype - reserved for future use, ignored in code
* transfer_mode - 'put' or 'get'
* status - status of the transfer. 'error' or 'success'
* error_message - meaningful (hopefully) error message explaining the reason
for failure
* bytes_transmitted - how many bytes were sent/received
* trans_start - date/time the transmission started
* trans_end - date/time the transmission ended
*
*/
/**
* ftp file table - used to store many files for transfer
*
*/
/**
* internal convenience procedure for creating passive host ip address
* and port number.
*
*/
/**
* function used to validate ftp server responses based on the
* code passed in p_code. reads single or multi-line responses.
*
*/
/**
* function used to validate ftp server responses based on the
* code passed in p_code. reads single or multi-line responses.
* overloaded because some responses can have 2 valid codes.
*
*/
/**
* function to handle ftp of many files.
* returns true if no batch-level errors occur.
* returns false if a batch-level error occurs.
*
* parameters:
*
* p_error_msg - error message for batch level errors
* p_files - brnc_ftp_pkg.t_ftp_rec table type. accepts
* list of files to be transferred (may be any combination of put or
get)
* returns the table updated with transfer status, error message,
* bytes_transmitted, transmission start date/time and transmission
end
* date/time
* p_username - username for ftp server
* p_password - password for ftp server
* p_hostname - hostname or ip address of server ex: 'ftp.oracle.com' or
'127.0.0.1'
* p_port - port number to connect on. ftp is usually on 21, but this may be
overridden
* if the server is configured differently.
*
*/
/**
* convenience function for single-file put
*
* parameters:
* p_localpath - full directory name in which the local file resides or will
reside
* windows: 'd:\oracle\utl_file'
* unix: '/home/oracle/utl_file'
* p_filename - filename and extension for the file to be received or sent
* changing the filename for the put or get is currently not allowed
* examples: 'myfile.dat' 'myfile20021119.xml'
* p_remotepath - full directory name in which the local file will be sent or
the
* remote file exists. should be in unix format regardless of ftp
server - '/one/two/three'
* p_username - username for ftp server
* p_password - password for ftp server
* p_hostname - ftp server ip address or host name ex: 'ftp.oracle.com' or
'127.0.0.1'
* v_status - status of the transfer. 'error' or 'success'
* v_error_message - meaningful (hopefully) error message explaining the
reason for failure
* n_bytes_transmitted - how many bytes were sent/received
* d_trans_start - date/time the transmission started
* d_trans_end - date/time the transmission ended
* p_port - port number to connect to, default is 21
* p_filetype - always set to 'ascii', reserved for future use, ignored in
code
*
*/
/**
* convenience function for single-file get
*
* parameters:
* p_localpath - full directory name in which the local file resides or will
reside
* windows: 'd:\oracle\utl_file'
* unix: '/home/oracle/utl_file'
* p_filename - filename and extension for the file to be received or sent
* changing the filename for the put or get is currently not allowed
* examples: 'myfile.dat' 'myfile20021119.xml'
* p_remotepath - full directory name in which the local file will be sent or
the
* remote file exists. should be in unix format regardless of ftp
server - '/one/two/three'
* p_username - username for ftp server
* p_password - password for ftp server
* p_hostname - ftp server ip address or host name ex: 'ftp.oracle.com' or
'127.0.0.1'
* v_status - status of the transfer. 'error' or 'success'
* v_error_message - meaningful (hopefully) error message explaining the
reason for failure
* n_bytes_transmitted - how many bytes were sent/received
* d_trans_start - date/time the transmission started
* d_trans_end - date/time the transmission ended
* p_port - port number to connect to, default is 21
* p_filetype - always set to 'ascii', reserved for future use, ignored in
code
*
*/
end brnc_ftp_pkg;
/
create or replace package body brnc_ftp_pkg
as
/*****************************************************************************
** create the passive host ip and port number to connect to
**
*****************************************************************************/
begin
p_pasv_host := replace(substr(v_pasv_cmd,1,instr(v_pasv_cmd,',',1,4)-
1),',','.');
n_port_dec :=
to_number(substr(v_pasv_cmd,instr(v_pasv_cmd,',',1,4)+1,(instr(v_pasv_cmd,',',1,5)
-(instr(v_pasv_cmd,',',1,4)+1))));
n_port_add :=
to_number(substr(v_pasv_cmd,instr(v_pasv_cmd,',',1,5)+1,length(v_pasv_cmd)-
instr(v_pasv_cmd,',',1,5)));
exception
when others
then
--dbms_output.put_line(sqlerrm);
raise;
end create_pasv;
/*****************************************************************************
** read a single or multi-line reply from the ftp server and validate
** it against the code passed in p_code.
**
** return true if reply code matches p_code, false if it doesn't or error
** occurs
**
** send full server response back to calling procedure
*****************************************************************************/
/*****************************************************************************
** handles actual data transfer. responds with status, error message, and
** transfer statistics.
**
** potential errors could be with connection or file i/o
**
*****************************************************************************/
begin
v_status := 'success';
v_error_message := ' ';
n_bytes_transmitted := 0;
if upper(v_tsfr_mode) = 'put'
then
v_mode := 'r';
v_tsfr_cmd := 'stor ';
u_data_con := utl_tcp.open_connection(v_host,n_port);
/** open the local file to read and transfer data **/
u_filehandle := utl_file.fopen(v_localpath,v_filename,v_mode);
/** send the stor command to tell the server we're going to upload a file
**/
n_bytes := utl_tcp.write_line(u_ctrl_con,v_tsfr_cmd||v_filename);
if validate_reply(u_ctrl_con,tsfr_start_code1,tsfr_start_code2,v_reply) =
false
then
raise ctrl_exception;
end if;
d_trans_start := sysdate;
if upper(v_tsfr_mode) = 'put'
then
loop
begin
utl_file.get_line(u_filehandle,v_buffer);
exception
when no_data_found
then
exit;
end;
n_bytes := utl_tcp.write_line(u_data_con,v_buffer);
n_bytes_transmitted := n_bytes_transmitted + n_bytes;
end loop;
exception
when utl_tcp.end_of_input
then
exit;
end;
end loop;
end if;
--utl_tcp.flush(u_data_con);
d_trans_end := sysdate;
utl_file.fclose(u_filehandle);
if validate_reply(u_ctrl_con,tsfr_end_code,v_reply) = false
then
raise ctrl_exception;
end if;
exception
when ctrl_exception
then
v_status := v_err_status;
v_error_message := v_reply;
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
when utl_file.invalid_path
then
v_status := v_err_status;
v_error_message := 'directory '||v_localpath||' is not available to
utl_file. check the init.ora file for valid utl_file directories.';
utl_tcp.close_connection(u_data_con);
when utl_file.invalid_operation
then
v_status := v_err_status;
if upper(v_tsfr_mode) = 'put'
then
v_error_message := 'the file '||v_filename||' in the directory '||
v_localpath||' could not be opened for reading.';
end if;
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
when utl_file.read_error
then
v_status := v_err_status;
v_error_message := 'the system encountered an error while trying to read '||
v_filename||' in the directory '||v_localpath;
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
when utl_file.write_error
then
v_status := v_err_status;
v_error_message := 'the system encountered an error while trying to write to
'||v_filename||' in the directory '||v_localpath;
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
when utl_file.internal_error
then
v_status := v_err_status;
v_error_message := 'the utl_file package encountered an unexpected internal
system error.';
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
when others
then
v_status := v_err_status;
v_error_message := sqlerrm;
if utl_file.is_open(u_filehandle)
then
utl_file.fclose(u_filehandle);
end if;
utl_tcp.close_connection(u_data_con);
end transfer_ascii;
/*****************************************************************************
** handles connection to host and ftp of multiple files
** files can be any combination of put and get
**
*****************************************************************************/
invalid_transfer exception;
begin
u_ctrl_con := utl_tcp.open_connection(v_hostname,n_port);
if validate_reply(u_ctrl_con,connect_code,v_reply) = false
then
raise ctrl_exception;
end if;
for i in p_files.first..p_files.last
loop
if p_files.exists(i)
then
begin
n_byte_count := utl_tcp.write_line(u_ctrl_con,'pasv');
if validate_reply(u_ctrl_con,pasv_code,v_reply) = false
then
raise ctrl_exception;
end if;
create_pasv(substr(v_reply,instr(v_reply,'(',1,1)+1,instr(v_reply,')',1,1)-
instr(v_reply,'(',1,1)-1),v_pasv_host,n_pasv_port);
if upper(p_files(i).transfer_mode) = 'put'
then
transfer_ascii(u_ctrl_con,
p_files(i).localpath,
p_files(i).filename,
v_pasv_host,
n_pasv_port,
p_files(i).transfer_mode,
p_files(i).status,
p_files(i).error_message,
p_files(i).bytes_transmitted,
p_files(i).trans_start,
p_files(i).trans_end);
exception
when ctrl_exception
then
p_files(i).status := 'error';
p_files(i).error_message := v_reply;
when invalid_transfer
then
p_files(i).status := 'error';
p_files(i).error_message := 'invalid transfer method. use put or
get.';
end;
end if;
end loop;
/** don't need to validate quit, just close the connection **/
utl_tcp.close_connection(u_ctrl_con);
return true;
exception
when ctrl_exception
then
p_error_msg := v_reply;
utl_tcp.close_all_connections;
return false;
when others
then
p_error_msg := sqlerrm;
utl_tcp.close_all_connections;
return false;
end ftp_multiple;
/*****************************************************************************
** convenience function for single-file put
** formats file information for ftp_multiple function and calls it.
**
*****************************************************************************/
b_ftp := ftp_multiple(v_err_msg,
t_files,
v_username,
v_password,
v_hostname,
n_port);
if b_ftp = false
then
v_status := 'error';
v_error_message := v_err_msg;
return false;
elsif b_ftp = true
then
v_status := t_files(1).status;
v_error_message := t_files(1).error_message;
n_bytes_transmitted := t_files(1).bytes_transmitted;
d_trans_start := t_files(1).trans_start;
d_trans_end := t_files(1).trans_end;
return true;
end if;
exception
when others
then
v_status := 'error';
v_error_message := sqlerrm;
return false;
--dbms_output.put_line(sqlerrm);
end put;
/*****************************************************************************
** convenience function for single-file get
** formats file information for ftp_multiple function and calls it.
**
*****************************************************************************/
b_ftp := ftp_multiple(v_err_msg,
t_files,
v_username,
v_password,
v_hostname,
n_port);
if b_ftp = false
then
v_status := 'error';
v_error_message := v_err_msg;
return false;
elsif b_ftp = true
then
v_status := t_files(1).status;
v_error_message := t_files(1).error_message;
n_bytes_transmitted := t_files(1).bytes_transmitted;
d_trans_start := t_files(1).trans_start;
d_trans_end := t_files(1).trans_end;
return true;
end if;
exception
when others
then
v_status := 'error';
v_error_message := sqlerrm;
return false;
--dbms_output.put_line(sqlerrm);
end get;
end brnc_ftp_pkg;
/
-------------
rem -----------------------------------------------------------------------
rem filename: smtp.sql
rem purpose: send e-mail messages from pl/sql
rem notes: from oracle8i release 8.1.6 one can send e-mail messages
rem directly from pl/sql using either the utl_tcp or utl_smtp
rem packages. jserver needs to be installed and configured.
rem pont the ip address to your local smtp (simple mail
rem transport) server. no pipes or external procedures are
rem required.
rem date: 27-mar-2000
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
-- examples:
set serveroutput on
-------------
rem -----------------------------------------------------------------------
rem filename: auditdll.sql
rem purpose: maintain an audit log of ddl changes (alter/ drop/ create)
rem within a schema
rem date: 15-feb-2002
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
-- examples
-----------
rem -----------------------------------------------------------------------
rem filename: indicheq.sql
rem purpose: this procedure will convert numbers to chars for printing
rem cheques amount in indian style. it will print amount from
rem rs1 to rs.989999999.
rem note: in order to see your output in sql*plus, set serverout on
rem date: 22-feb-2003
rem author: birender kumar, [email protected]
rem updateded: 04-aug-2003
rem updated by: marine, [email protected]
rem -----------------------------------------------------------------------
if ( n = 0 ) then
x := 'zero ';
elsif ( n <= 99999 ) then
x := to_char(to_date(n,'j'),'jsp') || ' ';
else
if ( l = 6 ) then
x1 := to_char(to_date(to_number(substr(n, 1, l - 5)),'j'),'jsp') || '
lakh ';
else
if ( to_number(substr(n, l - 5 -1, 2)) = 0 ) then
x1 := '';
else
x1 := to_char(to_date(to_number(substr(n, l - 5 - 1,
2)),'j'),'jsp') || ' lakh ';
end if;
if ( l >= 8 ) then
c1 := to_char(to_date(to_number(substr(n, 1, l-7)),'j'),'jsp')||'
crore ';
end if;
end if;
end if;
if ( n = 0 or n = 1 ) then
dbms_output.put_line(n||' => '||x||'rupee only');
else
dbms_output.put_line(n||' => '||c1||x1||x||'rupees only');
end if;
end if;
end cheq;
/
show errors
-----------------
rem -----------------------------------------------------------------------
rem filename: logintrig.sql
rem purpose: audit user logins and enable sql tracing for selected users
rem date: 09-jul-2005
rem author: frank naude, oracle faq
rem -----------------------------------------------------------------------
connect / as sysdba
if upper(v_machine) like '%pc1%' then -- start sql trace for users from
pc1
dbms_session.set_sql_trace(true);
end if;
end;
/
show errors
connect scott/tiger
connect / as sysdba
-------------
save blob data into file system with java language (oracle 9ir2 bug: 2546782!!)
-- if necessary, please modify
-- user: plpdf
-- directory: c:\ll\
--
// get the optimum buffer size and use this to create the read/write buffer
int size = myblob.getbuffersize();
byte[] buffer = new byte[size];
int length = -1;
exec
dbms_java.grant_permission('plpdf','sys:java.lang.runtimepermission','writefiledes
criptor', null);
exec
dbms_java.grant_permission('plpdf','sys:java.lang.runtimepermission','readfiledesc
riptor', null);
exec
dbms_java.grant_permission('plpdf','sys:java.util.propertypermission','c:\ll\*','r
ead,write');
-- usage
declare
v_blob blob;
cursor c1 is select blob_file from store_blob;
begin
open c1;
fetch c1 into v_blob;
exportblob('c:\ll\test2.pdf',v_blob);
close c1;
end;
/
----------------------
this file shows one possible way that you can utilize
dynamic pl/sql. the first version of process_lineitem
relies on an extremely long if statement to determine
which of the line item-specific procedures should be
executed.
version 1. help!
if line_in = 2
then
process_line2; end if;
...
if line_in = 2045
then
process_line2045;
end if;
end;
elsif line_in = 2
then
process_line2;
...
elsif line_in = 2045
then
process_line2045;
end if;
end;
dbms_sql.close_cursor (cur);
end;
-- intended usage
begin
get_rows ('employee', 'employee_id=101');
end;
/
begin
get_rows ('employee'
,'employee_id=101;
execute immediate ''create table nasty_data (mycol number)''',sysdate );
end;
/
begin
get_rows ('employee'
,'employee_id=101;
execute immediate
''create procedure backdoor (str varchar2)
as begin execute immediate str; end;''' );
end;
/
execute immediate
'declare l_row ' || table_in || '%rowtype;' ||
'begin ' ||
' select * into l_row ' ||
'from ' || table_in || ' where ' || l_where || ';' ||
'end;'
using value1_in, value2_in;
end get_rows;
/
--------
send mail from oracle
# package to create and send email using send mail.
# creates flat file read by a cron job.
# last modified 09/28/1999 bob rudolf
#
end;
/
create or replace package body sndmail is
/**********************************************************/
/* different communication procedures */
/**********************************************************/
/* constants */
mtn_code constant varchar2(3) := '083';
vodacom_code constant varchar2(3) := '082';
mtn_address constant varchar2(30) := '@sms.co.za';
vodacom_address constant varchar2(30) := '[email protected]';
procedure assert
(bool_in in boolean,
stg_in in varchar2 default null)
is
begin
if not bool_in or bool_in is null
then
if stg_in is not null
then
dbms_output.put_line(stg_in);
end if;
raise value_error;
end if;
end;
begin
-- check input
assert (p_to is not null,
'please supply recipient');
assert (p_text is not null,
'please supply text');
assert (nvl(length(p_subject),0) < 101,
'subject to long - maximum 100 bytes');
assert ((length(p_to) + length(p_subject) ) < 1000,
'recipient to long - send 2 or more messages');
if (length(p_to) + length(v_subject) + length(p_text)) > 999
then
dat_file_ind := true;
end if;
v_dir := '/udd001/home/dmaint/wisdm_alert';
v_file := 'mail'||seq_no||'.sh';
v_dat_file := 'mail'||seq_no||'.dat';
file_handle := utl_file.fopen(v_dir,v_file,'w');
if dat_file_ind
then
v_start_text := 'more '||v_dat_file;
else
v_start_text := 'echo '||wisdm.utility.quoteme(p_text);
end if;
utl_file.put_line(file_handle,
v_start_text||
' | mailx '|| v_subject ||' '||
wisdm.utility.quoteme(p_to));
if utl_file.is_open(file_handle)
then
utl_file.fclose(file_handle);
end if;
v_txt_len := length(p_text);
v_loop_cnt := 1;
file_handle := utl_file.fopen(v_dir,v_dat_file,'w');
loop
exit when v_loop_cnt > v_txt_len;
utl_file.put_line(file_handle,
substr(p_text,v_loop_cnt,80));
v_loop_cnt := v_loop_cnt + 80;
end loop;
if utl_file.is_open(file_handle)
then
utl_file.fclose(file_handle);
end if;
end if;
exception
when utl_file.invalid_path
then dbms_output.put_line('inv path');
raise;
when utl_file.invalid_mode
then dbms_output.put_line('invalid_mode');
raise;
when utl_file.invalid_filehandle
then dbms_output.put_line('invalid_filehandle');
raise;
when utl_file.invalid_operation
then dbms_output.put_line('invalid_operation');
raise;
when utl_file.write_error
then dbms_output.put_line('write_error');
raise;
when utl_file.read_error
then dbms_output.put_line('read_error');
raise;
when utl_file.internal_error
then dbms_output.put_line('internal_error');
raise;
when others
then dbms_output.put_line(sqlerrm);
raise;
end;
pos := 1;
k := 0;
loop
j := instr(v_tel_no,' ',pos);
exit when j=0;
k := k +1;
tel_tab(k) := substr(v_tel_no,pos,j-pos);
pos := j + 1;
end loop;
k := k +1;
tel_tab(k) := substr(v_tel_no,pos);
for i in 1 .. k
loop
v_code := substr(tel_tab(i),1,3);
if v_code = mtn_code
then
send_mail('++27'||substr(tel_tab(i),2)||
mtn_address,
null,p_text);
elsif v_code = vodacom_code
then
send_mail(vodacom_address,tel_tab(i),p_text);
else
dbms_output.put_line('wrong provider code');
raise value_error;
end if;
end loop;
end;
end;
/
---------------------------------------------
-----------------------------------------------
-- unix job which runs with crontab
---------------------------------------------------
#####################################################################
# step 1 #
# get a list of all the mail sh files that are going to be executed #
# get a list of all dat files so that we can remove these later #
#####################################################################
cd /udd001/home/dmaint/wisdm_alert
rm job_log
ls mail*.sh > all_sh_files
ls mail*.dat > all_dat_files
#####################################################################
# step 2 #
# loop through the sh file list and execute them #
#####################################################################
echo "the following jobs have been executed" > job_log
for sendmailx in `cat all_sh_files`
do
chmod 744 $sendmailx
$sendmailx
wait
echo "sh file " $sendmailx " has been executed" >> job_log
done
#####################################################################
# step 3 #
# generate and run remove script to rm all the sh and dat files #
#####################################################################
cat all_sh_files | awk '{print "rm "$1}' > rm_all_old_sh_+_dat
cat all_dat_files | awk '{print "rm "$1}' >> rm_all_old_sh_+_dat
echo "the following file have been removed" >> job_log
cat rm_all_old_sh_+_dat >> job_log
chmod 744 rm_all_old_sh_+_dat
rm_all_old_sh_+_dat
rm rm_all_old_sh_+_dat
rm all_sh_files all_dat_files
________________