简体   繁体   中英

Suppress SQL*PLUS error in batch script

I have a script db.bat as below:

@echo off

setlocal enabledelayedexpansion
 for /F "tokens=*" %%A in (user.txt) do (
    sqlplus -s %%A @fetch.sql  >> output.txt 

 )

where user.txt (list of all user details for which I need expiry date. this list may have around 40-50 rows) is:

dbuser/password@database1
readuser/p@ssw0rd@database1
adminuser/Pa$$word@database2
.......
.......
.......

and fetch.sql is:

set pagesize 20
set linesize 200

select username, expiry_date from user_users;
exit;

The problem I am facing here is, whenevey my script db.bat encounters any SQL ERRORS like given below, its not moving further and getting hanged at that point until I manually stop that.

SQL ERRORs :

ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified


ERROR:
ORA-28000: the account is locked 

I have checked that there is a WHENEVER SQLERROR command that works in this situation but don't know how I can use it here.

For those kinds of errors, SQL*Plus is 'hanging' at a username prompt, as it hasn't been able to connect. You don't see that because of the -s flag. By default it will allow three attempts, which is useful when running interactively, but isn't helpful when run from a script like this. You can make it exit after the failed login with the -l 'logon' option :

sqlplus -s -l %%A @fetch.sql  >> output.txt

Try this, when using the fetch.sql in a script, you need to set the termout to off. The error is still there, only that your script will continue to execute after it.

set pagesize 20
set linesize 200

whenever sqlerror continue 
set termout off 

select username, expiry_date from user_users;
exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM