2017-10-04 47 views
1

我試圖自動化將GNAF(澳大利亞的位置數據集)導入新數據庫的過程。 我遇到的問題是,當我嘗試到數據庫並重新創建它,我得到一個錯誤說數據庫正在使用中:暫停調用-SQLcmd查詢 - 正在使用的數據庫

Invoke-Sqlcmd : Cannot drop database "GNAF" because it is currently in use. At line:11 char:5 
+  Invoke-Sqlcmd -Query "IF EXISTS (SELECT name FROM master.dbo.sysd ... 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException 
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand Invoke-Sqlcmd : Database 'GNAF' already exists. Choose a different database name. At line:15 char:5 
+  Invoke-Sqlcmd -Query "CREATE DATABASE GNAF" -serverinstance $Serv ... 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException 
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand 

在活動監視器SSMS它表明:

enter image description here

這是被調用的函數:

function importQNAF { 

$ScriptPath = "C:\Users\owain.esau\Scripts\PowerShell\SQL-Server\TRIS Database Anon\" # change to pwd for live 
$QNAFpath1 = ($ScriptPath + "GNAF\G-NAF\G-NAF AUGUST 2017\Standard\") 
$QNAFpath2 = ($ScriptPath + "GNAF\G-NAF\G-NAF AUGUST 2017\Authority Code\") 
$GNAF_ImportData_Path = ($ScriptPath + "\SQLScripts\GNAF_ImportData.sql") 

(Get-Content $GNAF_ImportData_Path).replace("Powershell To Change#1", $QNAFpath1) | Set-Content $GNAF_ImportData_Path 
(Get-Content $GNAF_ImportData_Path).replace("Powershell To Change#2", $QNAFpath2) | Set-Content $GNAF_ImportData_Path 

## Drop and Create Table 
Invoke-Sqlcmd -Query "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='GNAF') 
         BEGIN 
         ALTER DATABASE GNAF SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 
         DROP DATABASE GNAF; 
         END 
        ;" -serverinstance $ServerAddress; 
Start-Sleep -s 5; 
Invoke-Sqlcmd -Query "CREATE DATABASE GNAF" -serverinstance $ServerAddress; 
Start-Sleep -s 5; 

Invoke-Sqlcmd -InputFile ($ScriptPath + "SQLScripts\GNAF_CreateTables.sql") -serverinstance $ServerAddress; 

try { 
    Invoke-Sqlcmd -InputFile ($ScriptPath + "SQLScripts\GNAF_ImportData.sql") -serverinstance $ServerAddress -querytimeout 0; 
} catch { 
    if ($error -match "Access is denied.") { 
     Write-Host "[!] ERROR: Access denied to GNAF folder." 
     Write-Host "----------------------------------------" 
     Write-Host "You have two options: " 
     Write-Host "" 
     Write-Host " 1) Give MSSQLSERVER full access to" $ScriptPath 
     Write-Host " 2) Copy the1 folder: " $ScriptPath"GNAF to the MSSQL Source folder (C:\Program Files\SQL Server)" 

     Do { 
      switch(Read-Host "Please choose an action (1 or 2)") { 
       1 { 
        $Acl = (Get-Item $ScriptPath).GetAccessControl('Access'); 
        $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT SERVICE\MSSQLSERVER", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"); 
        $Acl.SetAccessRule($Ar); 
        Set-Acl $ScriptPath $Acl; 
        importQNAF; 
       } 
       2 { Write-Host "Not coded yet"; } 
       default { "Invalid Entry, must be 1 or 2"; } 
      } 
     } Until ($choice -eq 1 -or $choice -eq 2) 
    } 
} 

(獲取內容$ GNAF_ImportData_Path).replace($ QNAFpath1,「鮑威rshell更改#1「)| Set-Content $ GNAF_ImportData_Path (Get-Content $ GNAF_ImportData_Path).replace($ QNAFpath2,「Powershell To Change#2」)|設置內容$ GNAF_ImportData_Path } 我使用實際數據導入到SQL服務器的腳本幾乎等同於一個本網頁上找到:

https://www.rednotebluenote.com/2016/04/importing-psma-geocoded-national-address-file-g-naf-to-sql-server/

它的工作原理在SSMS直接罰款。此外,我不認爲我的錯誤匹配正在拾取正確的錯誤,而是匹配所有錯誤。

---更新------------------------------------------ ----

當我在函數啓動時重新啓動MSSQLSERVER服務時,讓它運行。

現在的問題是,它超時:

Invoke-Sqlcmd : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 
BULK INSERT [MB_2011] FROM 'C:\Users\owain.esau\OneDrive\Scripts\PowerShell\SQL-Server\TRIS Database Anon\GNAF\G-NAF\G-NAF AUGUST 2017\Standard\ACT_MB_2011_psv.psv' WITH (FIELDTERMINATOR = '|', ROWTERMINATOR = 
'\n', FIRSTROW = 2, ROWS_PER_BATCH=100000, TABLOCK) 
At line:30 char:9 
+   Invoke-Sqlcmd -InputFile ($ScriptPath + "SQLScripts\GNAF_Imp ... 
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException 
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand 

它只能運行於約45秒鐘,然後這個錯誤彈出,實際的腳本正在運行大約需要3 - 5分鐘SSMA運行。

(該功能已經更新了一下)

+0

請停止重新啓動SQL服務器。您有其他問題通過重新啓動「解決」。這有點像用錘子擊打你的拇指來治療頭痛。當然,你的頭不會再受傷了,但這是否合理? – vonPryz

+0

更快速簡單的修復,因爲我知道如何從PowerShell重新啓動它,因爲這是一個本地主機上沒有數據庫,它不是一個問題。我把它切換到下面的建議。 –

回答

1

您需要終止數據庫中所有現有的開放連接。

這裏是最簡單的方法(這基本上是強制所有其它連接被殺死)

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='GNAF') 
    BEGIN 
    ALTER DATABASE GNAF SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 
    DROP DATABASE GNAF; 
    END 
; 
+0

感謝您的回覆,我剛剛重新啓動sql服務後發現這一點。我還必須將-querytimeout 0添加到第二個invoke-sqlcmd調用中。你的方法比重啓服務更好嗎? –

+0

它不需要服務重新啓動。 – gvee