sqlplus: get parameters of file.sql execution - oracle

From a bat file I am using a process which is calling SQL/Plus in the same way:
#echo off
start sqlplus user/password#instance_name file.sql <instance_name>
echo End of process
pause
Inside file.sql I have something like :
SET FEEDBACK OFF
SET UNDERLINE OFF
SET PAGESIZE 0
SET VERIFY OFF
SET SCAN ON
SET HEADING OFF
SET RECSEP OFF
SET TRIMSPOOL ON
SET FEEDBACK OFF
SET TERMOUT OFF
SET LINESIZE 1000
SET BLOCKTERMINATOR OFF
set instance_name=<FROM PARAMETER>
SET VERIFY ON
SET HEADING ON
EXIT
How can I get the name which I am passing as parameter in SQL/Plus?

Use &1
From the documentation the SQL/Plus command has the syntax:
SQLPLUS [ [Options] [Logon|/NOLOG] [Start] ]
Then the START parameter:
Start
#{url|file_name[.ext]} [arg ...]
Specifies the name of a script and arguments to run. The script can be called from the local file system or from a web server.
SQLPlus passes the arguments to the script as if executing the file using the SQLPlus START command. If no file suffix (file extension) is specified, the suffix defined by the SET SUFFIX command is used. The default suffix is .sql.
See the START command for more information.
Then for the START command:
arg ...
Data items you wish to pass to parameters in the script. If you enter one or more arguments, SQL*Plus substitutes the values into the parameters (&1, &2, and so forth) in the script. The first argument replaces each occurrence of &1, the second replaces each occurrence of &2, and so on.

Related

not able to suppress sqlplus output on terminal

I am using following script to delimited file using spool in the unix server. I ssh into it using putty and execute the shell script.
Here is shell script:
/oracle/app/oracle/product/19.0.0/dbhome_1/bin/sqlplus -s /nolog<<-EOF
conn user/pass
SET PAGESIZE 0
SET LINESIZE 32000
SET NUMWIDTH 127
SET FEEDBACK OFF
set echo off
set heading off
set headsep off
SET MARKUP CSV ON DELIMITER | quote off
set termout off
spool mc_format.dat
select * from mytable;
spool off
EOF
but I am not able to suppress the rows printing out on the terminal, and eventually on log when I run using cron.
Is there any setting I am missing out?
Right from the documentation
SET TERMOUT OFF
Controls the display of output generated by commands in a script that is executed with #, ## or START. OFF suppresses the display so that you can spool output to a file without displaying the output on screen. ON displays the output on screen. TERMOUT OFF does not affect output from commands you enter interactively or redirect to SQL*Plus from the operating system.
So instead of the here document, call the script using one of the start methods mentioned.

Passing parameter from window bat to UNIX shell script

i know topic seems very familiar and there are many answers given for same, but my issue is something else.I have a .bat file in which i am passing more then 10 parameters(I know limit is 9 but i need it).It seems to be working fine.Script is below-:
#ECHO OFF
setlocal EnableDelayedExpansion
set n=0
for %%a in (%*) do (
set vector[!n!]=%%a
set /A n+=1
)
SET sadminUser=%vector[0]%
SET sadminPassword=%vector[1]%
SET dsnProvider=%vector[2]%
SET dbUserName=%vector[3]%
SET schemaFilePath=%vector[4]%
SET ddldictschemalogPath=%vector[5]%
SET repositoryName=%vector[6]%
SET ddlimpschemalogPath=%vector[7]%
SET grantUserRole=%vector[8]%
SET userId=anyuser
SET host=myhost
SET password=password
SET datatable=%vector[9]%
SET indexTable=%vector[10]%
SET ddlimpschemalogPath2=%vector[11]%
echo %ddlimpschemalogPath2%
echo y | "C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% exit
"C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8sw/DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
Now as you see i am doing echo %ddlimpschemalogPath2% which is last parameter in array,and i can see correct output as well.
Problem is when i try to pass these parameters into UNIX Shell Script.You can see i am doing in bat file using putty command line Plink.I can successfully connect to shell script as well,And i am trying to echo all the passed parameters in shell script.But facing some issue . Script is below-:
#!/bin/bash
sadminUser=$1
sadminPassword=$2
dsnProvider=$3
dbUserName=$4
schemaFilePath=$5
ddldictschemalogPath=$6
repositoryName=$7
ddlimpschemalogPath=$8
grantUserRole=$9
datatable=${10}
indexTable=${11}
ddlimpschemalogPath2=${12}
echo "$sadmimUser"
echo "$sadminPassword"
echo "$ddlimpschemalogPath2"
echo password | sudo -S -l
sudo host << EOF
// Do something else
EOF
I Found one issue. In .bat i have parameter called -: repositoryName . When i do echo %repositoryName% it gives "Siebel Repository" correct output. Now when this spaced value is passed as parameter to shell script it breaks into 2 different values so:
These two parameter in script take 2 different values-:
repositoryName=$7
ddlimpschemalogPath=$8
Output-:
Siebel
repository
And it should be one value for parameter repositoryName=$7 . Giving value Siebel Repository. Why is this happening? Can this me issue why value are up and down?
As you can see in .bat i am making call to shell script, and passing parameters taken from .bat file as below-:
DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
You can see complete line above in .bat.
Invoking .bat file -:
dlSync.bat "SAD" "glob81" "gepf_DSN" "SIEBEL" "/global/u70/globepfdev/siebel/schema.ddl" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/expschem.log" "Siebel Repository" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync1.log" "SSE_ROLE" "GLOB_DATA_SMALL" "GLOB_INDEX_SMALL" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync2.log"
Same parameter are passing out to shell script . where "Siebel Repository" breaks into 2. Thanks

Run a program within a batch file

I need to make a batch file which will execute a series of commands within a program. I need it to connect to an oracle database and then initiate a pull.
Here is by current file:
sqlplus [username]/[pass]#[host]:[port]/[service]
set feedback off
set term off
set linesize 32000
set pagesize 0
set trimspool on
set underline off
set heading on
set newpage none
spool C:\1.csv CREATE
[Query Goes here]
I run the script, but it doesn't execute anything except the first line. It connects okay, but waits for me to quit sqlplus to execute the rest of the script. Is there a way to fix this? If possible I would like to avoid using multiple files.
If anyone comes across this question looking for a *nix based solution, it'd be something like this:
sqlplus -s <username>/password <<EOF
set feedback off
set term off
set linesize 32000
set pagesize 0
set trimspool on
set underline off
set heading on
set newpage none
spool C:\1.csv CREATE
EOF
The subsequent commands are sent to SQLPLUS until it encounters EOF, then SQLPLUS will close.
But I'm not sure how to do the equivalent on Windows.

Sqlplus command is not working via plink

I have a shell script in which I am using sqlplus command to fetch the data from database (Installed on Linux). same script is working fine on Linux environment. When I execute the script on window environment via using below batch file.
set ORACLE_TERM=xterm
set ORACLE_BASE=/home/pwcadm/app/pwcadm
set ORACLE_HOME=/home/pwcadm/app/pwcadm/product/11.2.0/client_1
set ORACLE_HOSTNAME=kyora02.kymab.local
set ORACLE_SID=orcl
set ORA_NLS11=$ORACLE_HOME/nls/data
set LANG=en_US.UTF-8
set PATH=/opt/CollabNet_Subversion/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/bin:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/bin:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/lib/site_perl/5.8.3/i686-linux-thread-multi:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/perl/lib/5.10.0/x86_64-linux-thread-multi:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/perl/bin:/home/pwcadm/Informatica/PowerCenter9.1.0.3/server/bin
set LD_LIBRARY_PATH=/home/pwcadm/app/pwcadm/product/11.2.0/client_1/lib:/home/pwcadm/app/pwcadm/product/11.2.0/client_1/lib32:/home/pwcadm/Informatica/PowerCenter9.1.0.3/server/bin
cd C:\Program Files\PuTTY
plink csaadm#172.16.122.11 -pw csaadm /app/csa/REG_AUTOMATION/scripts/CHECK_GAN_INSERT.sh
Batch file is prompting the below error message. Even I have set all the paths.
C:\Program Files\PuTTY>plink csaadm#172.16.122.11 -pw csaadm /app/csa/REG_AUTOMA
TION/scripts/CHECK_GAN_INSERT.sh
Error 6 initializing SQL*Plus
SP2-0667: Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
FAIL
/app/csa/REG_AUTOMATION/scripts/CHECK_GAN_INSERT.sh: line 27: [: -ne: unary oper
ator expected
CHECK_GAN_INSERT.sh
ACCOUNT_GLOBAL_COUNT=$(echo "
set heading off
set feedback off
set verify off
set trimspool on
set trimout off
set pagesize 0
set space 0
whenever sqlerror exit 2;
SELECT COUNT(*) FROM ACCOUNT_GLOBAL WHERE NAME='test';
"|/home/pwcadm/app/pwcadm/product/11.2.0/client_1/bin/sqlplus -S ${DB_USERNAME}/${DB_PASSWORD}#${CONNECTING_STRING})
EROR_MSG=$(echo ${ACCOUNT_GLOBAL_COUNT} |grep ORA|wc -l)
if [ ${EROR_MSG} -ne 0 ]
then
echo "${ACCOUNT_GLOBAL_COUNT}"
exit;
fi
if [ ${ACCOUNT_GLOBAL_COUNT} -ne 0 ]
then
echo "PASS"
else
echo "FAIL"
fi
Please help.
We haven't enough information about your problem, but here is my guess:
you have to set ORACLE_HOME env. var each time you log in (or add it in auto executable scripts),
you have to extend PATH too (use export PATH=$PATH:$ORACLE_HOME/bin),
also, may be you need to start Oracle instance.
Check these three points and update your question if your problem is still not solved.

Execute SQL Plus SQL command from batch-cmd window hangs

I am trying to execute a PL-SQL statemtn in a file and log the results to a log file. It works, but the DOS window hangs and I have to manually close it.
Am I ending the SqlPlus session incorrectly?
rem SET TERMOUT OFF;
SET SqlPlusExe=C:\Oracle\product\11203_32bit\CLIENT_1\bin\sqlplus.exe
SET MyUser=MyID
SET MyPassword=MyPwd
SET Host=RXODSDEV.CIGNA.COM
SET SqlFile=C:\Users\MyLanId\Desktop\SQLPlus\test.sql
SET LogFile=C:\Users\MyLanId\Desktop\SQLPlus\test.log
%SqlPlusExe% %MyUser%/%MyPassword%#%Host% #%SqlFile% >> %LogFile%
quit;
/
For Windows-scripting I recommend you to set the environment variable "LOCAL" ("TWO_TASK" on linux). The variable will specify you connection string.
set LOCAL=RXODSDEV.CIGNA.COM
REM Create the file to run
#(
echo connect %MyUser%/%MyPassword%
echo #%SqlFile%
echo exit^;
) > %TEMP%\run.sql
sqlplus /nolog #%TEMP%\run.sql >> out.log
But who is doing DOS scripting these days? Powershell is so much more fun!
Maybe you find my launcher script useful, take a look at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html

Resources