|
| 1 | +<#-------------------------------------------------------------------------- |
| 2 | +.SYNOPSIS |
| 3 | +Script for running T-SQL files in MS SQL Server |
| 4 | +Hang Zhang |
| 5 | +Built on a post by Andy Mishechkin at https://gallery.technet.microsoft.com/scriptcenter/The-PowerShell-script-for-2a2456c4 |
| 6 | +This script has been tested on PowerShell V3, and not tested on older versions. |
| 7 | + |
| 8 | +.DESCRIPTION |
| 9 | +
|
| 10 | +.\RunSQL_SQL_Walkthrough.ps1 |
| 11 | +
|
| 12 | +Users will be prompted to input parameters for: |
| 13 | +- name of Microsoft SQL Server with R Service instance |
| 14 | +- database name that you want to create and use in this walkthrough |
| 15 | +- path and name of the .csv file on the local machine to be loaded to the database |
| 16 | +- user name which has the previliges of creating database, tables, stored procedures, and uploading data to tables |
| 17 | +- password of users |
| 18 | + |
| 19 | +Examples. |
| 20 | + |
| 21 | +Execute on remote SQL Server Express with |
| 22 | +.\RunSQL_SQL_Walkthrough.ps1 |
| 23 | + |
| 24 | +---------------------------------------------------------------------------#> |
| 25 | + |
| 26 | +function DownloadAndInstall($DownloadPath, $ArgsForInstall, $DownloadFileType = "exe") |
| 27 | +{ |
| 28 | + $LocalPath = [IO.Path]::GetTempFileName() + "." + $DownloadFileType |
| 29 | + $web_client.DownloadFile($DownloadPath, $LocalPath) |
| 30 | + |
| 31 | + Start-Process -FilePath $LocalPath -ArgumentList $ArgsForInstall -Wait |
| 32 | +} |
| 33 | + |
| 34 | +function InstallSQLUtilities(){ |
| 35 | + # Install SQL Server Command Line Utilities |
| 36 | + $b = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “Microsoft SQL Server" -and $_.Name -match "Command Line Utilities" } |
| 37 | + if($b -eq $null) |
| 38 | + { |
| 39 | + Write-Output "SQL Server Command Line Utilities not installed. Download and install SQL Server Command Line Utilities" -ForegroundColor "Yellow" |
| 40 | + $os = Get-WMIObject win32_operatingsystem |
| 41 | + $os_bit = $os.OSArchitecture |
| 42 | + if($os_bit -eq '64-bit') |
| 43 | + { |
| 44 | + $download_url1 = "http://go.microsoft.com/fwlink/?LinkID=188401&clcid=0x409" |
| 45 | + $download_url2 = "http://go.microsoft.com/fwlink/?LinkID=188430&clcid=0x409" |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + $download_url1 = "http://go.microsoft.com/fwlink/?LinkID=188400&clcid=0x409" |
| 50 | + $download_url2 = "http://go.microsoft.com/fwlink/?LinkID=188429&clcid=0x409" |
| 51 | + } |
| 52 | + Write-host "Installing SQL Server Native Client..." -ForegroundColor "Yellow" |
| 53 | + DownloadAndInstall $download_url1 "/quiet IACCEPTSQLNCLILICENSETERMS=YES" "msi" |
| 54 | + Write-host "Installing SQL Command Line Utilities..." -ForegroundColor "Yellow" |
| 55 | + DownloadAndInstall $download_url2 "/quiet" "msi" |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function SearchBCP(){ |
| 60 | + $bcp_list = Get-ChildItem -Path "C:\Program Files*\" -Filter bcp.exe -Recurse -ErrorAction SilentlyContinue -Force | where {$_.FullName -like '*\bcp.exe'} |
| 61 | + |
| 62 | + if ($bcp_list -ne $null){ |
| 63 | + $bcp_path = @('')*$bcp_list.count |
| 64 | + for ($i=0; $i -lt $bcp_list.count; $i++){ |
| 65 | + $bcp_path[$i] = $bcp_list[$i].DirectoryName |
| 66 | + } |
| 67 | + } |
| 68 | + return $bcp_path |
| 69 | +} |
| 70 | + |
| 71 | +function ExecuteSQLFile($sqlfile,$go_or_not) |
| 72 | +{ |
| 73 | + if($go_or_not -eq 1) |
| 74 | + { |
| 75 | + $SQLCommandText = @(Get-Content -Path $sqlfile) |
| 76 | + foreach($SQLString in $SQLCommandText) |
| 77 | + { |
| 78 | + if($SQLString -ne "go") |
| 79 | + { |
| 80 | + #Preparation of SQL packet |
| 81 | + if($SQLString -match "SET @path_to_data") |
| 82 | + { |
| 83 | + $SQLPacket += "SET @path_to_data = '" + $csvfilepath + "'`n" |
| 84 | + } |
| 85 | + Elseif($SQLString.ToLower() -match "set @db_name") |
| 86 | + { |
| 87 | + $SQLPacket += "set @db_name = '" + $dbname + "'`n" |
| 88 | + } |
| 89 | + Elseif($SQLString -match "SET @db_name") |
| 90 | + { |
| 91 | + $SQLPacket += "SET @db_name = " + $dbname + "`n" |
| 92 | + } |
| 93 | + Elseif($SQLString.ToLower() -match "use \[taxinyc_sample") |
| 94 | + { |
| 95 | + $SQLPacket += "USE [" + $dbname +"]`n" |
| 96 | + } |
| 97 | + Else |
| 98 | + { |
| 99 | + $SQLPacket += $SQLString + "`n" |
| 100 | + } |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + Write-Host "---------------------------------------------" |
| 105 | + Write-Host "Executed SQL packet:" |
| 106 | + Write-Host $SQLPacket |
| 107 | + $IsSQLErr = $false |
| 108 | + #Execution of SQL packet |
| 109 | + try |
| 110 | + { |
| 111 | + $SQLCommand = New-Object System.Data.SqlClient.SqlCommand($SQLPacket, $SQLConnection) |
| 112 | + $SQLCommand.CommandTimeout = 0 |
| 113 | + $SQLCommand.ExecuteScalar() |
| 114 | + } |
| 115 | + catch |
| 116 | + { |
| 117 | + |
| 118 | + $IsSQLErr = $true |
| 119 | + Write-Host $Error[0] -ForegroundColor Red |
| 120 | + $SQLPacket | Out-File -FilePath ($PWD.Path + "\SQLErrors.txt") -Append |
| 121 | + $Error[0] | Out-File -FilePath ($PWD.Path + "\SQLErrors.txt") -Append |
| 122 | + "----------" | Out-File -FilePath ($PWD.Path + "\SQLErrors.txt") -Append |
| 123 | + } |
| 124 | + if(-not $IsSQLErr) |
| 125 | + { |
| 126 | + Write-Host "Execution succesful" |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + Write-Host "Execution failed" -ForegroundColor Red |
| 131 | + } |
| 132 | + $SQLPacket = "" |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + #Reading the T-SQL file as a whole packet |
| 139 | + $SQLCommandText = @([IO.File]::ReadAllText($sqlfile)) |
| 140 | + #Execution of SQL packet |
| 141 | + try |
| 142 | + { |
| 143 | + $SQLCommand = New-Object System.Data.SqlClient.SqlCommand($SQLCommandText, $SQLConnection) |
| 144 | + $SQLCommand.CommandTimeout = 0 |
| 145 | + $SQLCommand.ExecuteScalar() |
| 146 | + } |
| 147 | + catch |
| 148 | + { |
| 149 | + Write-Host $Error[0] -ForegroundColor Red |
| 150 | + } |
| 151 | + } |
| 152 | + #Disconnection from MS SQL Server |
| 153 | + |
| 154 | + Write-Host "-----------------------------------------" |
| 155 | + Write-Host $sqlfile "execution done" |
| 156 | +} |
| 157 | + |
| 158 | +###################### End of Functions / Start of Script ###################### |
| 159 | + |
| 160 | +$server = Read-Host -Prompt 'Input the database server name (the full address)' |
| 161 | +$dbname = Read-Host -Prompt 'Input the name of the database you want to create' |
| 162 | +$u = Read-Host -Prompt 'Input the user name which has the previlige to create the database' |
| 163 | +$p0 = Read-Host -Prompt 'Input the password of user name which has the previlige to create the database' -AsSecureString |
| 164 | +$p1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($p0) |
| 165 | +$p = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($p1) |
| 166 | +$csvfilepath = Read-Host -Prompt 'Input the path to the csv file you want to upload to the database' |
| 167 | + |
| 168 | +# Check whether BCP is intalled on the computer. If no, install it. |
| 169 | +try |
| 170 | +{ |
| 171 | + $bcp_path = SearchBCP |
| 172 | + if ($bcp_path -eq $null){ |
| 173 | + Write-Host "bcp.exe is not found in C:\Program Files*. Now, start installing SQL Utilities..." -ForegroundColor "Yellow" |
| 174 | + InstallSQLUtilities |
| 175 | + $bcp_path = SearchBCP |
| 176 | + } |
| 177 | + Write-Host "Adding path to bcp.exe to the system path..." -Foregroundcolor "Yellow" |
| 178 | + $env_path = $env:Path |
| 179 | + for ($i=0; $i -lt $bcp_path.count; $i++){ |
| 180 | + if ($bcp_path.count -eq 1){ |
| 181 | + $bcp_path_i = $bcp_path |
| 182 | + } else { |
| 183 | + $bcp_path_i = $bcp_path[$i] |
| 184 | + } |
| 185 | + if ($env_path -notlike ‘*’+$bcp_path_i+'*'){ |
| 186 | + Write-Host $bcp_path_i 'not in system path, add it...' |
| 187 | + [Environment]::SetEnvironmentVariable("Path", "$bcp_path_i;$env_path", "Machine") |
| 188 | + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") |
| 189 | + $env_path = $env:Path |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +catch |
| 194 | +{ |
| 195 | + Write-Host "Installing SQL Utilities failed. " |
| 196 | +} |
| 197 | + |
| 198 | +#Connect to MS SQL Server |
| 199 | +try |
| 200 | +{ |
| 201 | + $SQLConnection = New-Object System.Data.SqlClient.SqlConnection |
| 202 | + #The MS SQL Server user and password is specified |
| 203 | + if($u -and $p) |
| 204 | + { |
| 205 | + $SQLConnection.ConnectionString = "Server=" + $server + ";Database=master;User ID= " + $u + ";Password=" + $p + ";" |
| 206 | + } |
| 207 | + #The MS SQL Server user and password is not specified - using the Windows user credentials |
| 208 | + else |
| 209 | + { |
| 210 | + $SQLConnection.ConnectionString = "Server=" + $server + ";Database=master;Integrated Security=True" |
| 211 | + } |
| 212 | + $SQLConnection.Open() |
| 213 | +} |
| 214 | +#Error of connection |
| 215 | +catch |
| 216 | +{ |
| 217 | + Write-Host $Error[0] -ForegroundColor Red |
| 218 | + exit 1 |
| 219 | +} |
| 220 | + |
| 221 | +# Create database and tables, and upload data to database from local machine using BCP |
| 222 | +Write-Host "Start creating database and table on your SQL Server, and uploading data to the table. It may take a while..." |
| 223 | +$start_time = Get-Date |
| 224 | +try |
| 225 | +{ |
| 226 | + ExecuteSQLFile $PWD"\create-db-tb-upload-data.sql" 1 |
| 227 | +} |
| 228 | +catch |
| 229 | +{ |
| 230 | + Write-Host "Creating database and tables failed. Probably the database or tables already exist." -ForegroundColor "Red" |
| 231 | +} |
| 232 | + |
| 233 | +$db_tb = $dbname + ".dbo.nyctaxi_sample" |
| 234 | +Write-host "start loading the data to SQL Server table..." -Foregroundcolor "Yellow" |
| 235 | +try |
| 236 | +{ |
| 237 | + bcp $db_tb in $csvfilepath -t ',' -S $server -f taxiimportfmt.xml -F 2 -C "RAW" -b 200000 -U $u -P $p |
| 238 | +} |
| 239 | +catch |
| 240 | +{ |
| 241 | + Write-Host "BCP uploading data to table failed. Please check whether bcp.exe is installed and the path is added to system path." -ForegroundColor "Red" |
| 242 | +} |
| 243 | +$end_time = Get-Date |
| 244 | +$time_span = $end_time - $start_time |
| 245 | +$total_seconds = [math]::Round($time_span.TotalSeconds,2) |
| 246 | +Write-Host "This step (creating database and tables, and uploading data to table) takes $total_seconds seconds." -Foregroundcolor "Yellow" |
| 247 | + |
| 248 | +Write-Host "Start running the .sql files to register all functions and stored procedures used in this walkthrough..." |
| 249 | +$start_time = Get-Date |
| 250 | +ExecuteSQLFile $PWD"\fnCalculateDistance.sql" 1 |
| 251 | +ExecuteSQLFile $PWD"\fnEngineerFeatures.sql" 1 |
| 252 | +ExecuteSQLFile $PWD"\TrainTipPredictionModel.sql" 1 |
| 253 | +ExecuteSQLFile $PWD"\PlotHistogram.sql" 1 |
| 254 | +ExecuteSQLFile $PWD"\PlotInOutputFiles.sql" 1 |
| 255 | +ExecuteSQLFile $PWD"\PredictTip.sql" 1 |
| 256 | +ExecuteSQLFile $PWD"\PredictTipSingleMode.sql" 1 |
| 257 | +Write-Host "Completed registering all functions and stored procedures used in this walkthrough." |
| 258 | +$end_time = Get-Date |
| 259 | +$time_span = $end_time - $start_time |
| 260 | +$total_seconds = [math]::Round($time_span.TotalSeconds,2) |
| 261 | +Write-Host "This step (registering all functions and stored procedures) takes $total_seconds seconds." |
| 262 | +$SQLConnection.Close() |
0 commit comments