![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to include RETURN KEY with Background process "&" in Shell Script | racbern | Shell Programming and Scripting | 1 | 03-11-2008 04:30 AM |
| passing a list of dynamic names to a "PS" command in shell script? | sachin.tendulka | Shell Programming and Scripting | 3 | 11-21-2007 01:18 AM |
| Q: Recording shell script screen output using "script" command ? | lalfonso.gomez | Shell Programming and Scripting | 4 | 01-18-2007 06:31 PM |
| Shell Script fails with "can't connect to client" | sseenu79 | HP-UX | 2 | 12-20-2006 07:47 AM |
| No utpmx entry: you must exec "login" from lowest level "shell" | peterpan | UNIX for Dummies Questions & Answers | 0 | 01-18-2006 01:15 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
[Oracle] "Dynamic" sql / Shell script
Greetings!
I wrote this shiny #!/usr/bin/ksh ... # Declare environment parameters, logons, functions... #----------------------------------------------------# . $HOME/lib/ETL.lib ... # Gets TODAY'S DATE from main db instance. # This will be used to manipulate at runtime the WHERE clause of a SQL \ # statement that gets run. #------------------------------------------------------# rcode1=`${ORACLE_HOME}/bin/sqlplus -s <<EOF1 ${SRC_ORACLE_LOGON} # DB uname/password in .lib set heading off; select to_char(sysdate, 'MMDDYYYY') from dual; exit; EOF1` # Gets YESTERDAY'S DATE from main db instance. # This will be used to manipulate at runtime the WHERE clause of a SQL \ # statement that gets run. #-------------------------------------------------------# rcode2=`${ORACLE_HOME}/bin/sqlplus -s <<EOF2 ${SRC_ORACLE_LOGON} # DB uname/password in .lib set heading off; select to_char(sysdate-1, 'MMDDYYYY') from dual; exit; EOF2` ... # Sets properties of datum1 & datum1 to rcode1 & rcode2. # This will initialize the conditions (i.e. dates) in the WHERE clause \ # as well as the file name the query results get spooled out to. #---------------------------------------------------------# export rcode1 rcode2 ; export ORACLE_SID=another_oracle_db_instance_name ; export filler="080000" ; export datum1="$rcode1$filler" ; export datum2="$rcode2$filler" ... # Set properties for file management. #----------------------------------------------------------# export suffix=".uld" ; export label1="SALES_REVENUE_" # Download of SALES REVENUE data. # Exporting `salesrevdatafname` will allow building dynamically \ # the output file name with a timestamp I obtained earlier. #----------------------------------------------------------# export salesrevdatafname="$label1$rcode3$suffix" ${ORACLE_HOME}/bin/sqlplus -s <<! > $LOG/sql.og ${TGT_ORACLE_LOGON} # DB uname/password in .lib SET ARRAYSIZE 5000 COLSEP '|' spool $DATA_TGT/$salesrevdatafname; SELECT ... FROM SALES WHERE trx_start_time >= to_date('$datum2','MMDDYYYYHH24MISS') AND trx_start_time < to_date('$datum1','MMDDYYYYHH24MISS'); spool off; exit; ! ... That's it. The main reason why I am posting this is that I went thru a lot of trial & error to get what I was looking for (this). Who knows, this code may be of help to someone with similar need. NB: I was able to tweak this code to make it run on IBM DB2 on AIX as well. At the risk of sounding like a pompous *ss...This bit of code is in the public domain. Modify it as you please. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Nice job especially since it works so well for you.
One question. Why are you exporting every one of your variables? By the way, you can give yourself a bit of a short cut and avoid the unnecessary second sqlplus session by using a little different technique. This one uses an array. Code:
set -A DATE_ARRAY $(
sqlplus -s /nolog <<EOF
connect user/password
set pages 0 feedback off verify off echo off head off
select to_char(sysdate, 'MMDDYYYY') || '235959'
,to_char(sysdate-1, 'MMDDYYYY') || '000000'
from dual;
EOF
)
sqlplus /nolog <<EOF > /dev/null
connect user/password@yourotherdatabase
set ... your output parameters
spool ...
SELECT ...
FROM yourtable
WHERE date_column between to_date('${DATE_ARRAY[2]}', 'MMDDYYHH24MISS')
and to_date('${DATE_ARRAY[1]}', 'MMDDYYHH24MISS');
spool off
EOF
Code:
sqlplus /nolog <<EOF > /dev/null
connect user/password@yourotherdatabase
set ... your output parameters
REM This will create variables that can be used in the second SQL
COLUMN BDATE NEW_VALUE BDATE
COLUMN EDATE NEW_VALUE EDATE
SELECT to_char(sysdate, 'MMDDYYYY') || '235959' EDATE
,to_char(sysdate-1, 'MMDDYYYY') || '000000' BDATE
FROM DUAL;
spool yourfilename&&BDATE...
SELECT ...
FROM yourtable
WHERE date_column between to_date('&&BDATE', 'MMDDYYHH24MISS')
and to_date('&&EDATE', 'MMDDYYHH24MISS');
spool off
EOF
Last edited by tmarikle; 10-05-2005 at 08:19 AM. Reason: Left out the real point of example 2 where the variable is used for the filename. |
|
#3
|
||||
|
||||
|
[Oracle] "Dynamic" sql / Shell script
Quote:
1) Configuration management 1.A) I first code/test my work using the env variables of my dev/qa server/environment such Oracle DB user name/password, Oracle TNS name, directory name(s) etc. 1.B) One my qa is completed, I push my shell script to my prod server. The variables still have identical names as in `dev`; however, I can/do change the values of each variable to match my `prod` environment e.g. Oracle DB user name/password, Oracle TNS name, directory name(s) etc. 2) Security 2.A) The shell scripts have a chmod value which allows anybody that's not me (ie. my Unix id) to read the code. However, the chmod value of my ETL.LIB file make it so that it can only view/modified by me and the root user. Stuff like passwords, file/directory names etc is hidden from most people. 3) Simplicity 3.A) I have 214 shell scripts like the one I posted. Some are wrappers to SQL*Loader jobs, others to SQL*scripts...It is a lot easier for me to manage changes by editing one single file (ETL.LIB...as opposed to each one of them) whenever our DBA feels like changing passwords, TNS names or when our sysadmin decides to mount a new file system etc... That's about it. PS: Thanks for the kind comments. |
|
#4
|
||||
|
||||
|
Quote:
I like this. it is really clever example since you're able to accompliish the same thing as me in one single shot. Thanks for your input!! Alan |
||||
| Google The UNIX and Linux Forums |