簡體   English   中英

DBATOOLS-從表中復制數據並在DestinationDatabase上創建

[英]DBATOOLS - Copy Data from table and create on DestinationDatabase

DbaTools相關- https://dbatools.io/functions/

您好,請嘗試復制表並在-Destination / -DestinationDatabase上創建表的解決方案。

我在用:

Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"

但是由於表不是在目標上創建的,因此出現錯誤消息:

警告:[15:25:58] [Copy-DbaTableData] Table01在目標位置不存在

請問有什么方法可以在DestinationDatabase上復制和創建表?

這是我用於類似任務的內容-

# Configuration for scripting options: Which related objects should also be scripted? 
$so = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions; 
$so.DriAllConstraints = $true; 
$so.DriAllKeys = $true;
$so.DriClustered = $true; 
$so.DriDefaults = $true; 
$so.DriIndexes = $true; 
$so.DriNonClustered = $true; 
$so.DriPrimaryKey = $true; 
$so.DriUniqueKeys = $true; 
$so.AnsiFile = $true; 
$so.ClusteredIndexes  = $true; 
$so.IncludeHeaders = $true; 
$so.Indexes = $true; 
$so.SchemaQualify = $true; 
$so.Triggers = $true; 
$so.XmlIndexes = $true; 

#Hard-Coding the Server and database info
$SourceServer = "SourceServer"
$SourceDatabase = "SourceDatabase"
$TargetServer = "TargetServer"
$TargetDatabase = "TargetDatabase"


#Creating folders for storing Create_Database text files if they don't exist
If(!(Test-Path E:\ScriptDatabases)) 
{
    New-Item -ItemType Directory -Force -Path E:\ScriptDatabases
}

#Creating the database connection object for Source server
$Srcsrv = new-Object Microsoft.SqlServer.Management.Smo.Server($Sourceserver);
$Srcdb = New-Object Microsoft.SqlServer.Management.Smo.Database;
$Srcdb = $Srcsrv.Databases.Item($SourceDatabase);

#Creating the database connection object for Destination server
$Destsrv = new-Object Microsoft.SqlServer.Management.Smo.Server($TargetServer);
$Destdb = New-Object Microsoft.SqlServer.Management.Smo.Database;
$Destdb = $Destsrv.Databases.Item($TargetDatabase);

foreach ($table in $Srcdb.Tables)
{
    $table.Script($so) | Out-File -FilePath E:\ScriptDatabases\$($table.Name).txt
    $CreatedbQuery = Get-Content E:\ScriptDatabases\$($table.Name).txt | Out-String
    Invoke-sqlcmd -Query $CreatedbQuery -Database $TargetDatabase -server $TargetServer
}

上面的腳本將從源數據庫到文本文件的所有表腳本化,然后從文本文件中讀取它們,並在目標數據庫和目標服務器上創建表。 沒什么要記住的-

  1. 該腳本要求數據庫存在於目標服務器中。 如果不是,腳本將失敗。 因此,在執行此操作之前,請創建目標數據庫。
  2. 如果您的數據庫中除了dbo之外還有其他任何模式,那么您需要首先創建這些模式。 否則,將不會為其他模式創建您的表。
  3. 有關腳本選項的列表,您可以參考 msdn鏈接,您可以在其中根據需要將數據庫對象的腳本選項設置為$true$false

您可以擁有此Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01" Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"在創建表后運行,或者您可以在創建表本身之后在腳本中循環執行,以便同時進行表創建和數據加載到表中。 這完全是您的選擇,我將自行決定。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM