Requirements Description
Generally in the production environment, in the case of put into operation, need batch to execute SQL script file, to complete the whole production, if the production file more than the case, undoubtedly this is a more painful process, so this article through PowerShell script bulk completed.
Monitoring scripts
<#批量执行SQL脚本文件 #><#===========================================#>$serverInstance="WUXUEL1" $Database="111"# $userName="sa" # $password="password01!" $ScriptPath="C:\powershell\SQLTest\" $ScriptList=""<#===========================================#>$n="' N" $r="' R" while($ScriptList. IndexOf ($n)-Gt0) {$ScriptList=$ScriptList.Replace($n, ";")} while($ScriptList. IndexOf ($r)-Gt0) {$ScriptList=$ScriptList.Replace($r, ";")} while($ScriptList. IndexOf ("")-Gt0) {$ScriptList=$ScriptList.Replace(" ","")} while($ScriptList. IndexOf (",")-Gt0) {$ScriptList=$ScriptList.Replace(",","")}If($ScriptList. IndexOf (". sql") Le0) {$ScriptList="" [System.IO.DirectoryInfo]$DirectoryInfo=New-Object System.IO.DirectoryInfo $ScriptPath|Sort-Object foreach ($finch($DirectoryInfo. GetFiles ("*. sql "))) {$ScriptList=$ScriptList+";"+$f. Name}} try{[void][System.Reflection.Assembly]:: LoadWithPartialName ('Microsoft.SqlServer.ConnectionInfo')|Out-NULL$ServerConnection=New-Object Microsoft.SqlServer.Management.Common.ServerConnection # $serverInstance, $userName, $password $ Serverconnection.connectionstring=The Data Source=$serverInstance; Initial Catalog=$Database; Integrated Security=True "try {$ServerConnection. BeginTransaction () Write-Host "BeginTransaction." [System.Text.StringBuilder]$Sql="" Foreach ($File inch$ScriptList. Split (";")) { if($File -NE "") {Write-Host $ScriptPath $File"... start" $Sql=$Sql. Appendline ([System.Io.File]:: OpenText ($ScriptPath+$File)). ReadToEnd ()) $ServerConnection. ExecuteNonQuery ($SQL)|Out-NULL$SQL="" Write-Host $ScriptPath $File " ... Ok! "}} $ServerConnection. CommitTransaction () Write-Host "CommitTransaction." } Catch {If($ServerConnection. transactiondepth-Gt0{$ServerConnection. RollbackTransaction () Write-Host "RollbackTransaction." } Write-Error $_}} catch{Write-Error $_}
It involves several parameters that need to be configured:
1, the root directory path of the batch file
2. This script supports two kinds of authentication methods: User authentication & Windows authentication, as needed to determine
The implementation completion report is as follows:
Of course, the most important thing is, if the execution process, a script error problem solved, in fact, only need to mark the wrong file name and the wrong information can be.
Adjust the script as needed, adjust it to the correct, and then re-execute the script.
Scheduling scripts
Sometimes our deployment typically schedules window periods, which means that the deployment of the script needs to be performed at a certain time of the night.
To solve this problem, PowerShell provides two basic scheduling methods:
1, through the SQL Server Agent to establish the appropriate job timed to execute to complete the deployment.
2, through the Windows scheduled task to execute the schedule.
The above two methods are relatively simple, the online information a lot, we look for themselves.
SQL Server Automation Operations series--Batch Execute SQL script (Power Shell)