DBATOOLS - 从 table 复制数据并在 DestinationDatabase 上创建
DBATOOLS - Copy Data from table and create on DestinationDatabase
DbaTools 相关 - https://dbatools.io/functions/
您好,
我正在尝试找到一个解决方案来复制 table 并在 -Destination / -DestinationDatabase
上创建它。
我正在使用:
Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
但是由于 table 没有在目标上创建,我收到错误消息:
WARNING: [15:25:58][Copy-DbaTableData] Table01 does not exist on
destination
有什么方法可以在 DestinationDatabase 上复制和创建 table 吗?
这是我用于类似任务的东西 -
# 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
}
上面的脚本会将源数据库中的所有 tables 编写成文本文件,然后从文本文件中读取相同的内容,并在目标数据库和目标服务器上创建 tables。需要牢记的几件事 -
- 脚本要求数据库存在于目标服务器中。如果不是,则脚本失败。所以在执行这个之前创建你的目标数据库。
- 如果您的数据库中除了
dbo
之外还有任何其他模式,那么您需要先创建这些模式。否则您的 table 将不会创建其他模式。
- 有关脚本选项列表,您可以参考 this msdn link,您可以在其中根据您的需要将数据库对象的脚本选项设置为
$true
或 $false
要求。
您可以在创建 table 之后使用此 Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
运行,或者您可以在创建 table 之后在脚本中循环,以便 table 创建数据和将数据加载到 table 中是同时发生的。这完全是您的选择,我将由您自行决定。希望这对您有所帮助!
DbaTools 相关 - https://dbatools.io/functions/
您好,
我正在尝试找到一个解决方案来复制 table 并在 -Destination / -DestinationDatabase
上创建它。
我正在使用:
Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
但是由于 table 没有在目标上创建,我收到错误消息:
WARNING: [15:25:58][Copy-DbaTableData] Table01 does not exist on destination
有什么方法可以在 DestinationDatabase 上复制和创建 table 吗?
这是我用于类似任务的东西 -
# 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
}
上面的脚本会将源数据库中的所有 tables 编写成文本文件,然后从文本文件中读取相同的内容,并在目标数据库和目标服务器上创建 tables。需要牢记的几件事 -
- 脚本要求数据库存在于目标服务器中。如果不是,则脚本失败。所以在执行这个之前创建你的目标数据库。
- 如果您的数据库中除了
dbo
之外还有任何其他模式,那么您需要先创建这些模式。否则您的 table 将不会创建其他模式。 - 有关脚本选项列表,您可以参考 this msdn link,您可以在其中根据您的需要将数据库对象的脚本选项设置为
$true
或$false
要求。
您可以在创建 table 之后使用此 Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
运行,或者您可以在创建 table 之后在脚本中循环,以便 table 创建数据和将数据加载到 table 中是同时发生的。这完全是您的选择,我将由您自行决定。希望这对您有所帮助!