If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to execute a ananomyous pl/sql block using shell script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-11, 15:34
MIKELALA MIKELALA is offline
Registered User
 
Join Date: Nov 2009
Posts: 45
Red face How to execute a ananomyous pl/sql block using shell script

Hello Friends ,

I have a pl/sql block that should be executed by autosys job scheduler .

For the scheduler to execute , the pl/sql block needs to be written as .sh script.

The following is the pl/sql block . Can any one let me know how to create the same in .sh file.

=========================

DECLARE
TYPE tbl_col_type is TABLE of VARCHAR2(50);
L_table_col tbl_col_type := tbl_col_type('X_REV_M02_RSTM');
l_sql_stmt VARCHAR2(32000);
BEGIN
FOR rec IN L_table_col.FIRST..L_table_col.LAST
LOOP
l_sql_stmt := 'UPDATE '||'KAW_OWNER.'||L_table_col(rec)||
' SET X_REVENUE_TYPE_CD = REPLACE(X_REVENUE_TYPE_CD, ''-'', ''_'')'||
' WHERE INSTR(X_REVENUE_TYPE_CD, ''-'') > 0' ;
EXECUTE IMMEDIATE l_sql_stmt ;
END LOOP;
END;
====================
The autosys job scheduler runs the .sh file

thanks/mike
Reply With Quote
  #2 (permalink)  
Old 01-20-11, 15:52
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Put the pl/sql into a SQL file, then call sqlplus from within your shell script passing the SQL file as an argument. More details are in the SQL*Plus manual

Last edited by shammat; 01-20-11 at 18:13.
Reply With Quote
  #3 (permalink)  
Old 01-20-11, 17:17
spacebar spacebar is offline
Registered User
 
Join Date: Feb 2006
Posts: 73
This is just a simple shell script(run_update_sql.sh) example you can try/modify:
Code:
#!/bin/ksh
# run_update_sql.sh

ORACLE_ACCESS=user/pass\@host
LOG_FILE_PATH=$HOME/logs
LOG_FILE=/run_update_sql.log
SCHEMA="SCHEMA_NAME"

###########################################################################################
#    Main Shell Processing
###########################################################################################

# Run SQL
SQL=`sqlplus -S <<RUN_UPDATE_SQL 2>>${LOG_FILE_PATH}${LOG_FILE}
$ORACLE_ACCESS
set pagesize 0 termout off
whenever sqlerror exit 1
  declare
    type tbl_col_type is table of varchar2(   50 );
    l_table_col tbl_col_type := tbl_col_type('X_REV_M02_RSTM');
    l_sql_stmt                    varchar2( 2000 );
  begin
    for rec in l_table_col.first..l_table_col.last
    loop
      l_sql_stmt := 'update '||'kaw_owner.'||l_table_col(rec)||
                    ' set x_revenue_type_cd = replace(x_revenue_type_cd, ''-'', ''_'')'||
                    ' where instr(x_revenue_type_cd, ''-'') > 0';
      execute immediate l_sql_stmt;
    end loop;
  end;
exit 0
RUN_UPDATE_SQL`
if [[ $? = 1 ]] then
  print `date "+%Y-%m-%d-%H.%M.%S"` "**Error running update sql. review above messages." >> ${LOG_FILE_PATH}${LOG_FILE}
fi
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On