Search code examples
sql-serverbatch-filesqlcmd

Streamlining a batch file calling a sql file


We currently have a scheduler to run audits. This scheduler will call a batch file with a parameter, and the batch file calls a sql script (based on the parameter), which in turn calls a stored proc.

For each audit that runs, a separate batch file, and sql file. Best case I'd like to combine the two below files into 1 file that can be used for every new audit. Worst case I'd at least like to combine to get 1 file for each audit instead of two. Hopefully you all can help?

Batch File

@echo on
Echo Running SQL Command File for '%1' Data Audit Check

del "D:\Internal_Reports\%1\%1.txt"

sqlcmd -S localhost -d database -i "D:\DataAudit\%1.sql"  -s "," > D:\Temp\%1.csv -I -W -k 1

if %ERRORLEVEL% GTR 0 COPY "D:\Temp\%1.csv" "D:\Internal_Reports\%1\%1.txt"

if %ERRORLEVEL% NEQ 0 goto Error_1
echo No Errors
goto end

:Error_1
if %ERRORLEVEL% NEQ 1 goto Error_2
echo No Errors
goto end

:Error_2
echo Errorlevel %ERRORLEVEL% 
set FileName=%1%2
echo Filename %FileName%
echo %ERRORLEVEL% > D:\ErrorLevel\%FileName%
EXIT /B %ERRORLEVEL%
:end

SQL File

set NoCount on
DECLARE
@createdBy varchar(16),

@dataAuditBatchId int, 

@createdDtTm datetime

select 
@createdBy = 'AutomatedAudit'
exec CreateNewDataAuditBatch @createdBy, @dataAuditBatchId output

-- Content Scripts
exec specificAuditStoredProc @createdBy, @dataAuditBatchId
select * from vAuditErrors where JobName in ('specificAuditStoredProc')
:exit(select Case When Counter = 0 then 0 Else 1 End 'errorCode'
from (select CAST(Count(*) AS varchar(4)) AS Counter from vAuditErrors 
    where JobName in ('specificAuditStoredProc'))
CountTable
)

Solution

  • Your best bet would be PowerShell in this case. You can combine both worlds of Batch Scripting and direct access to SQL.

    Copy the below code into a text file: Audit.ps1

    Create a File called: AuditFile.txt, put your SpecificAuditProc names on each line.

    Then in your batch scheduler run this: "powershell -command "& c:\Audit.ps1 -name 'ProcName'"

    Heres the code [Untested]:

           param([Parameter(Mandatory=$true)][String]$name="")
    
    $createdBy = "AutomatedAudit"
    
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server=LOCALHOST;Database=HT;Integrated     Security=True"
    $SqlConnection.Open()  
    
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection
    $SqlCmd.CommandText = "[CreateNewDataAuditBatch]"
    $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
    $SqlCmd.Parameters.Add("@createdBy", $creadtedBy)
    $SqlCmd.Parameters.Add("@dataAuditBatchId ")
    $SqlCmd.Parameters["@dataAuditBatchId"].Direction = [system.Data.ParameterDirection]::Output
    $SqlCmd.ExecuteNonQuery()
    $dataAuditBatchId = $Command.Parameters["@dataAuditBatchId"].value
    $SqlCmd.Dispose() 
    
    
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection
    $SqlCmd.CommandText = "[$name]"
    $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
    $SqlCmd.Parameters.Add("@createdBy", $creadtedBy)
    $SqlCmd.Parameters.Add("@dataAuditBatchId ", $dataAuditBatchId)
    $SqlCmd.ExecuteNonQuery()
    $SqlCmd.Dispose() 
    
    $sqlcheck = @(Invoke-Sqlcmd -Query "select * from vAuditErrors where JobName in ('$name')" -ServerInstance "LOCALHOST\HT")
    if ($sqlcheck.Count -ne 0) {
    $sqlcheck > D:\Internal_Reports\$name\$name.txt
    $sqlcheck.Count >> D:\ErrorLevel\$name
    }
    
    $Connection.Close()
    $Connection.Dispose()