Midnight ghost within Taskscheduler - sql

I am running into very strange behavior of scheduled code:
my code runs as expected by manual call within the cmd (it echos result = 1)
it still runs fine when I schedule it over the day
it does not run as expected at the night (it echos result = 0)
I set a variable in sqlplus and pass it into the Batch script errorlevel. then I echo the error level. I can only imagine that I overwrite my errorlevel at some point but I do not see why this should behave differently over the day.
batch script
#setlocal enableextensions Enabledelayedexpansion
#echo off
set logfile=test_log.log
set dbuser=some_guy#some_db
echo 1_start test... >> !logfile!
sqlplus !dbuser! test.sql >> !logfile!
echo CMD Return Value is: !errorlevel! >> !logfile!
echo 2_start test... >> !logfile!
sqlplus !dbuser! test.sql
echo CMD Return Value is: !errorlevel! >> !logfile!
endlocal
SQL File
variable return_value number
set serveroutput on
SET LINESIZE 10000
declare
null;
begin
:return_value := 1;
dbms_output.enable;
dbms_lock.sleep(1);
dbms_output.put_line('SQL return_value: ' || :return_value );
exception
when others then
:return_value := 5;
end;
/
set serveroutput off
exit :return_value
task-scheduler
run test.cmd at 3 AM and 11 AM
As mentioned above. I simply wanna see "CMD Return Value is: 1" somewhere in the log file. this does work fine, but not at night schedule. I did test some settings in the scheduler (e.g. configure for win-server 2012 or win 2007) - it did not change the result: At day time my return value is 1; at night it is 0.
I am using Windows Server 2012 and Oracle 19
Thank you for your help!

Related

Shell Scripting - Anonymous Block

I have a procedure that does a certain task and exits.
The procedure has to be replicated to around 40 databases, that will run this procedure.
My concern is that if there is a change all across, every procedure has to be changed.
How can I create a shell script, that takes input
username, password and SID and runs an anonymous block (the same procedure is converted as an anonymous block and put on server)
and it runs it.
Here is the code I sometimes use:
dbs_list="
DB1.USR1
DB2.USR2
"
for x in ${dbs_list}
do
DB_=` echo ${x}|cut -f "1" -d . `
USR=` echo ${x}|cut -f "2" -d . `
echo "################################ #### "
echo "#processinb ${USR} # ${DB_} -- ...#"
echo "enter password:"
read -s PWD
sqlplus ${USR}/${PWD}#${DB_} << _EOF
set serveroutput on
prompt HERE is my anonymous code block
begin
dbms_output.put_line('do my things');
end;
/
_EOF
done
Note the importance:
to have this / thing at the end of the PLSQL code block
to have this last _EOF thing at the very beginning of the line after the sqlplus pseudo-file block (or so called famous Here-document )
Hope this helps.

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;

How to capture sqlplus command line output in unix file

I am running below : sqlplus ABC_TT/asfddd#\"SADSS.it.uk.hibm.sdkm:1521/UGJG.UK.HIBM.SDKM\"
afte that I am executing one stored procedure exec HOLD.TRWER
I want to capture return code of the above stored procedure in unix file as I am running the above commands in unix. Please suggest.
I guess you are looking for spool
SQL> spool output.txt
SQL> select 1 from dual;
1
----------
1
SQL> spool off
Now after you exit. the query/stroed procedure output will be stored in a file called output.txt
If by return code you mean output then:
command > file
If by return code you mean exit status then:
command
echo "$?" > file
If you mean something else, let us know.
You can store command return value in variable
value=`command`
And then checking it's value
echo "$value"
For your case to execute oracle commands within shell script,
value=`sqlplus ABC_TT/asfddd#\"SADSS.it.uk.hibm.sdkm:1521/UGJG.UK.HIBM.SDKM\" \
exec HOLD.TRWER`
I'm not sure about the sql query, but you can get the returned results by using
value=`oraclecommand`.
To print the returned results of oracle command,
echo "$value"
To check whether oracle command or any other command executed successfully, just
check with $? value after executing command. Return value is 0 for success and non-zero for failure.
if [ $?=0 ]
then
echo "Success"
else
echo "Failure"
fi

save the result of a operation into a variable within a .BAT file

I have the following BAT file working ok:
it connects to an specific sql server and select getdate()
what I really wanted is to test whether the server is connectable.
I would like something like:
set is_connectable = call sqlcmd.exe %%SERVITORE%%
is there any way I could achieve this?
thanks and regards
marcelo
#echo off
color fc
cls
echo.
#ECHO --- BEGIN THE SCRIPT 1 ----
echo.
#echo the SERVITORE is "%1"
echo.
echo.
if "%1"=="" GOTO USAGE
set SERVITORE=-Stcp:%1% -Q " USE MASTER select getdate() "
call sqlcmd.exe %%SERVITORE%%
color 6
GOTO THE_END
:USAGE
echo.
echo USAGE:
echo.
ECHO the first parameter is the SERVITORE server.
echo example 1 SERVITORE108
echo.
ECHO the second parameter is optional
echo but if not supplied the default is \\SERVITORE\folder2$
echo.
echo ATB
echo.
:THE_END
color 8
It is easy to run a query and set the result to a DOS environment variable. For instance, you can do the following to get the date/time from the SQL Server instance (SQL Server is running locally in my instance):
for /f "skip=2 delims=" %%i in ('sqlcmd -S localhost -E -Q "set nocount on; select getdate() as [Now]"') do set is_connectable=%%i
However, the is_connectable environment variable set in this example will have an arbitrary value, which will make it hard to evaluate. Since you are just trying to verify that the SQL Server is there, alive, and responsive, you should run a query that creates a more predictable output, like this:
#echo off
:: Make sure the variable is undefined to start with
set is_connectable=
:: Make the connection and run a query that should always return '1'
for /f "skip=2 delims= " %%i in ('sqlcmd -S localhost -E -Q "set nocount on; select 1 as [Rows] into #temp; select ##rowcount as [Rows]; drop table #temp"') do set is_connectable=%%i
:: Verify if SQL Server is avaialble
if not defined is_connectable goto NotFound
if "%is_connectable%"=="1" goto Found
goto UnknownError
:Found
echo SQL Server was found and returned '1' as expected...
goto TheEnd
:NotFound
echo SQL Server was not found...
goto TheEnd
:UnknownError
echo SQLServer was found, but the return value was '%is_connectable%' instead of '1'...
goto TheEnd
:TheEnd

How to write batch (*.bat) script to execute Teradata query using BTEQ?

Below is the content of my script.bat :
#echo off
cd C:\Program Files\Teradata\Client\13.0\bin
bteq .LOGON server/username,password;
select date;
.LOGOFF
#echo off goto end
:end #echo exit
I have no problem with the logon, but it seems that bteq can't read my query statement:
select date;
It keeps prompting for input. Can anyone help me to get bteq to read and execute the query statement?
I've tried the solutions online about input and output file:
bteq <myscript.txt> mylog.log
but it didn't work either.
You are going to need two files, one is a batch and the other one are the commands.
Batch file:
echo off
cd C:\bteq_directory\
bteq < c:\commands.txt > c:\output.txt 2>&1
#echo off goto end
:end #echo exit
Commands:
.LOGON server/username,password
select date;
.LOGOFF
EDIT:
Removed semicolon after .LOGON...
This is a bit late, but here is what I've found:
I save my BTEQ script in a file called BTScript.txt:
.LOGON <servername>/<username>,<password>;
.SET WIDTH 20000;
.SET separator '|';
.EXPORT FILE = C:\TEMP\testBTEQ.txt;
SELECT top 10 * ATABLENAME;
.LOGOFF
.EXIT
The setting of width is needed to prevent the data being truncated, it will not go to 20000 chars if the record isnt that long.
To run this I execute (my pc is set up to allow me to double click on the file in windows explorer to do this) on a .bat file the content of which are:
echo off
bteq < C:\temp\BTScript.txt > c:\temp\bteqscriptout.txt 2>&1
#echo off goto end
:end #echo exit
The second file referred to here will contain the output from BTEQ.