Pages

Monday, August 10, 2015

ini file, batch, sqlplus by example

============================
General
============================
Example of reading ini file, and per each entry call sqlplus.

main.bat is reading from ini file.
Per each entry in ini file call run_sql.bat is called.
run_sql.bat is wrapping a call to sqlplus.
sqlplus would generate an output file.

So per each entry in ini file, an output file is generated.
This file is then appended into one common file.

============================
code
============================
db_list.ini
main.bat
run_sql.bat
stats_log_sql.sql

============================
db_list.ini
============================
USER_A/PASS_B@CONN_STR_A
USER_B/PASS_B@CONN_STR_B
USER_C/PASS_C@CONN_STR_C
USER_D/PASS_D@CONN_STR_D
USER_E/PASS_E@CONN_STR_E
USER_F/PASS_F@CONN_STR_F
USER_G/PASS_G@CONN_STR_G
USER_H/PASS_H@CONN_STR_H
USER_J/PASS_J@CONN_STR_J

============================
main.bat
============================
ECHO OFF
setlocal
cls

SET inifile=db_list.ini
SET STATS_LOG=stats_log.txt
SET TEMP_STATS_LOG=temp_stats_log.txt

ECHO.
ECHO =============================================
ECHO main is starting
ECHO This Program read entries from file %inifile% and report results to %STATS_LOG%
ECHO =============================================
ECHO.

ECHO. 2>%TEMP_STATS_LOG%
ECHO. 2>%STATS_LOG%

FOR /F "tokens=*" %%i IN (%inifile%) DO (
    ECHO handling %%i
    call run_sql.bat %TEMP_STATS_LOG% %%i
    TYPE %TEMP_STATS_LOG% >> %STATS_LOG%
ECHO.
)

ECHO.
ECHO =============================================
ECHO main has finished
ECHO =============================================


============================
run_sql.bat
============================
SET spool_to_file=%1
SET db_name=%2
REM ECHO got DB name: %db_name%
REM ECHO stats_log_sql %db_name%
sqlplus -l -s %db_name% @stats_log_sql %spool_to_file% %db_name%

============================
stats_log_sql.sql
============================
SET TERMOUT ON
SET SHOW OFF
SET VERIFY OFF 
SET HEAD ON
SET LINE 500
SET FEEDBACK OFF
SET PAGES 500
SET TRIMS ON

SPOOL &&1
SELECT '&&2' AS GATE_NAME, COUNT(*) AS STATS_LOG_ENTRIES FROM STATS_LOG;
SPOOL OFF
EXIT;
/

No comments:

Post a Comment