Today, We have been working on a service request that our customer needs to implement a connection retry-logic using SQLCMD. Following, I would like to share some ideas for this implementation.
The first thing that we need to know if we might have two issues using SQLCMD connecting to the database and executing the query, for this reason, I'm going to run the following statement saving it in a .BAT file:
@echo off
SET /A "index = 1"
SET /A "count = 10"
SET /A "waitfor = 5"
SET /A "size = 0"
:while
if %index% leq %count% (
echo ------------------------------------------------------------------------
echo Retry-Logic - Attempt Number %index%
if exist C:\PERFCHECKER\OutPut_%index%.TXT (
DEL /f /q C:\PERFCHECKER\OutPut_%index%.TXT
echo Retry-Logic - Deleted file C:\PERFCHECKER\OutPut_%index%.TXT
timeout 1 /nobreak
)
if exist C:\PERFCHECKER\OutPut_%index%.LOG (
DEL /f /q C:\PERFCHECKER\OutPut_%index%.LOG
echo Retry-Logic - Deleted file C:\PERFCHECKER\OutPut_%index%.LOG
timeout 1 /nobreak
)
echo Retry-Logic - Executing SQLCMD
sqlcmd -S tcp:servername.database.windows.net,1433 -U username -P password -l60 -t3600 -d databasename -b -Q "EXEC ExecuteThis" -o C:\PERFCHECKER\Output_%index%.TXT
echo Retry-Logic - Executed SQLCMD Error Level Result %errorlevel%
if %errorlevel% EQU 0 (
echo Retry-Logic - Checking if the file exist C:\PERFCHECKER\OutPut_%index%.TXT
IF exist C:\PERFCHECKER\OutPut_%index%.TXT (
echo Retry-Logic - Reading file C:\PERFCHECKER\OutPut_%index%.TXT File result %errorlevel%
FINDSTR "ProcessFinishedWithSuccess" C:\PERFCHECKER\OutPut_%index%.TXT >C:\PERFCHECKER\Output_%index%.LOG
if %errorlevel% EQU 0 (
echo Retry-Logic - Checking if the file exist C:\PERFCHECKER\OutPut_%index%.LOG
IF exist C:\PERFCHECKER\OutPut_%index%.LOG (
FOR %%A IN (C:\PERFCHECKER\Output_%index%.LOG) DO set size=%%~zA
echo Retry-Logic - File C:\PERFCHECKER\OutPut_%index%.log Size is %size%
if %size% neq 0 (
set /p Build=<C:\PERFCHECKER\Output_%index%.LOG
set errorRetryLogic=%Build:~0,26%
echo Retry-Logic - : Build %Build%
echo Retry-Logic - : Return %errorRetryLogic%
if "%errorRetryLogic%" == "ProcessFinishedWithSuccess" (
echo Retry-Logic - Finished
goto :end)
)
)
)
)
)
SET /A "waitfor = waitfor + (index*2)"
timeout %waitfor% /nobreak
SET /A "index = index + 1"
goto :while
)
:end
As an example, I wrote the following TSQL that contains the store procedure that I'm using to capture the results. The last line contains the text that I'm using to know if the execution was completed correctly.
CREATE PROCEDURE ExecuteThis
AS
BEGIN
SET NOCOUNT ON
SELECT TOP 60 * FROM DEMO
SELECT 'ProcessFinishedWithSuccess'
END
Enjoy!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.