Skip to content

Instantly share code, notes, and snippets.

@davidlares
Last active August 31, 2023 02:27
Show Gist options
  • Save davidlares/a19a9d80be89cba09883a6a003c33934 to your computer and use it in GitHub Desktop.
Save davidlares/a19a9d80be89cba09883a6a003c33934 to your computer and use it in GitHub Desktop.
PoC of Powershell and Batch (.bat) scripts for A2 softway (SQLserver 64-bit) [Untested]
DROP TABLE [dbo].[Sinventario];
@echo off
REM This is the Main function
:main
SET invTable=Sinventario
CALL :checkingDatabase %1 %2
EXIT /B %ERRORLEVEL%
REM Checking if the database exists
:checkingDatabase
for /f %%a in ('sqlcmd -S %~1 -d %~2 -h 1 -r 1 -Q "SELECT COUNT(*) FROM sys.databases WHERE name = '%~2'" ^| findStr /r "[^(][0-9]"') do (
SET /A dbExist=%%a
)
REM evaluating database count
if %dbExist% gtr 0 (
echo [Program] The database exists
CALL :checkInventoryExistence %~1 %~2
)
EXIT /B 0
REM Checking if the table exists
:checkInventoryExistence
for /f %%a in ('sqlcmd -S %~1 -d %~2 -h 1 -r 1 -Q "SELECT count(*) FROM sys.tables WHERE name = '%invTable%'" ^| findStr /r "[^(][0-9]"') do (
SET /A tableExist=%%a
)
REM evaluating table count
if %tableExist% gtr 0 (
echo [Program] The Inventory table exist, removing.
CALL :droppingTables %~1 %~2
)
CALL :executingPkg
EXIT /B 0
REM Dropping existing inventory tables
:droppingTables
echo [Program] Dropping mirrored SQLServer tables
sqlcmd -S %~1 -d %~2 -i %~dp0exec.sql
EXIT /B 0
REM Execution SSIS SQLServer package
:executingPkg
echo [Program] Running SSIS Package
FORFILES /P %~dp0 /M *.dtsx /C "cmd /c dtexec /Rep E /F @file"
EXIT /B 0
@echo off
REM This is the Main function
:main
CALL :checkingDatabase %1 %2
EXIT /B %ERRORLEVEL%
REM Checking if the database exists
:checkingDatabase
for /f %%a in ('sqlcmd -S %~1 -d %~2 -h 1 -r 1 -Q "SET nocount on; SELECT COUNT(*) FROM sys.databases WHERE name = '%~2'" ^| findStr /r "[^(][0-9]"') do (
SET /A dbExist=%%a
)
REM evaluating database count
if %dbExist% gtr 0 (
echo [Program] The database exists
CALL :checkInventoryExistence %~1 %~2
)
EXIT /B 0
REM Checking if the table exists
:checkInventoryExistence
for /f %%a in ('sqlcmd -S %~1 -d %~2 -h 1 -r 1 -Q "SET nocount on; SELECT count(*) FROM sys.tables WHERE name = 'Sinventario'" ^| findStr /r "[^(][0-9]"') do (
SET /A tableExist=%%a
)
REM evaluating table count
if %tableExist% gtr 0 (
echo [Program] The Inventory table exist, removing.
CALL :droppingTable %~1 %~2
)
CALL :executingPkg
EXIT /B 0
REM Dropping existing inventory tables
:droppingTable
echo [Program] Dropping mirrored SQLServer tables
sqlcmd -S %~1 -d %~2 -i "exec.sql"
EXIT /B 0
REM Execution SSIS SQLServer package
:executingPkg
echo [Program] Running SSIS Package
FORFILES /P %CD% /M *.dtsx /C "cmd /c dtexec /Rep E /F @file"
EXIT /B 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment