Execute SQL Plus SQL command from batch-cmd window hangs - windows

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

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.

Run sqlplus commands from multiple files with query logging or spool in Windows Batch File

I am quite new to batch scripting, and I am trying to run multiple sql files, which in turn may contain multiple sql DML/DDL queries from bat file. The output files must contain all the queries being executed and the query output. Unlike this example , I don't have spool command inside my sql file, and I can not edit the input sql files. The following command works for me in ksh file (thanks to here-document):
$sqlplus /nolog <<! >>$sqlLogs.lst
connect $USERNAME/$PASSWORD#${DBNAME}
set echo on timing on
spool ${SCRIPTRUNFILE_SPOOL}
select name from v\$database;
#${SCRIPTRUNFILE};
spool off
exit
!
I want the exact same in Windows bat file. I have tried using ^. I can't do combine all sql files into one, as I need logging for each sql file into different file. My attempt at the bat file script is as follows and I have played around this much, and it fails with spool command not recognized. I also prefixed below commands with sqlplus, but still unable to achieve something like above ksh file:
sqlplus -s username/pwd#DBName >> sqlLogs.lst
set echo on timing on
spool %RUNFILENAME%.lst
#%RUNFILENAME% > %RUNFILENAME%.lst
select name from v\$database;
spool off
quit
Following logic executes my scripts but does not log the query being executed. Also, I don't want to connect twice to the database.
echo select name from v$database; | sqlplus -s username/pwd#DBName >> sqlLogs.lst
echo quit | sqlplus -s username/pwd#DBName #%SCRIPTRUNFILE%>> %SCRIPTRUNFILE_SPOOL%.lst
Can someone here please help to spool to a file where I can log the queries as well, while maintaining a single DB Connection?
Pass your immediate SQL*Plus commands to your sqlplus via stdout, grouped together by Windows' ( and ) symbols...
(
echo.set echo on timing on
echo.spool %SCRIPTRUNFILE_SPOOL%
echo.select name from v$database;
echo.#%SCRIPTRUNFILE%
echo.spool off
echo.exit
) | sqlplus -s %USERNAME%/%PASSWORD%#%DBNAME% >> sqlLogs.lst

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.

Oracle Script File Execution in *.bat file not completing

I have the following *.bat file executing itself on a schedule. This file consists of different tasks, primarily involves in having a the backup dump file exported to the Test DB Machine every day.
#echo off
#echo %time%
del C:\Factory\index.sql
echo exporting indexes...
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump_index.log indexfile=index.sql
#echo %time%
echo connecting to Oracle...
echo Delete and Re-Creation of User
sqlplus "sys/password as sysdba" #C:\Factory\script\step_1.sql
echo importing dump file begins...
#echo %time%
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump.log Compile=n INDEXES=n
echo Applying security priviledges
#echo %time%
sqlplus "sys/password as sysdba" #C:\Factory\script\step_2.sql
#echo %time%
ssr 0 "CONNECT" "REM CONNECT" C:\Factory\index.sql
echo Loading Indexes....`enter code here`
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
#echo %time%
exit
shutdown.exe -r -t 00
Since its a long operation, I execute itself on every night, expecting it to be ready next morning. I also include a command restart the machine once the entire process is over. All the command finishes without asking an input expect the following command:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
after the above command executes, the batch file asks for an input to exit the process as the snapshot below says
Can anybody help in automatically finish the job and restart itself, rather than ask for an input after the index.sql file is loaded?
Create a file named quit.txt, inside it put the text "quit".
Where you see the lines that execute sqlplus, like this one:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
change them to this:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql < quit.txt
Old stuff, but however: Let "exit;" be the last command of your SQL script. This tells sqlplus to quit and finish execution.

Using Batch Script connect to oracle db run a select command and output all data to a flat file

I am trying to create a batch file for windows (XP) which will have several sqls and when its run it would
1. connect with oracle
2. set the userid/password/schema
3. run each sql in the loop and
4. output each sql outputs to its own flat file.
I have started the script
#ECHO off
SET STATE=fl
TABLE1=AGENCY
set SQL1="SELECT Column_ID||CHR(31)||column_ENTITY_CD||CHR(31) FROM AGENCY"
set TABLE2=FIRM
set SQL2="SELECT Column_ID||CHR(31)||Column_NM||CHR(31) FROM FIRM"
set TABLE3=FL_CO_LOB
Set SQL3="SELECT Column_ID||CHR(31)||Column_LOB_CODE||CHR(31) FROM FL_CO_LOB"
...
SET NumberOfTables=19
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /l %%A IN (1,1,%NumberOfTables%) DO (
echo !SQL%%A!
)
endlocal
I can get the SQL out of the variable but don't have any clue how to connect to oracle and run the sql and get the output to a defined file.
please give me some direction.
one thing to notice that the echo is printing including the double quote. but If i don't have them then it just print the 1st word not the whole query.
Thanks
If you've installed the oracle client on your workstation, you have SQLPlus.exe for command line work with the database. Your TNSNAMES.ORA file needs to be up to date, and tnsping needs to be able to locate your service.
I think you'll need to change your approach. The scripts you want to run will need to be fed to the SQLPlus.exe program, rather than being delivered semi-interactively.
I suggest creating a .sql script instead of creating evnironment variables
Your batch file line might look like:
#ECHO off
SET STATE=fl
set TABLE1=AGENCY
set TABLE2=FIRM
echo SELECT Column_ID^|^|CHR(31)^|^|column_ENTITY_CD^|^|CHR(31) FROM %TABLE1% > tablecommands.sql
echo SELECT Column_ID^|^|CHR(31)^|^|Column_NM^|^|CHR(31) FROM %TABLE2% >> tablecommands.sql
echo SELECT Column_ID^|^|CHR(31)^|^|Column_LOB_CODE^|^|CHR(31) FROM FL_CO_LOB >> tablecommands.sql
...
<until your 19 SQL statements are declared>
SQLPlus User/Password#Database #tablecommands.sql
Add the sqlplus login command before your sql commands in your batch file. It executes and writes to logs file where the batch file resides
syntax : sqlplus -s ora_user_name/ora_user_password [as sysdba]#ora_sid#"path_to_sql_file" > output.log
ex. sqlplus -s scott/tiger#xe #"D:\Oralcle\scripts\sql_file.sql" > output.txt

Resources