[英]DBATOOLS - Copy Data from table and create on DestinationDatabase
DbaTools Related - https://dbatools.io/functions/ DbaTools相关- https://dbatools.io/functions/
Hello, I am trying to find a solution to copy a table and create it on the -Destination / -DestinationDatabase
, please. 您好,请尝试复制表并在
-Destination / -DestinationDatabase
上创建表的解决方案。
I am using: 我在用:
Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
But the since the table is not created on the destination, I get error message: 但是由于表不是在目标上创建的,因此出现错误消息:
WARNING: [15:25:58][Copy-DbaTableData] Table01 does not exist on destination
警告:[15:25:58] [Copy-DbaTableData] Table01在目标位置不存在
Is there any way I copy and create the table on DestinationDatabase, please? 请问有什么方法可以在DestinationDatabase上复制和创建表?
This is something which I had used for a similar task - 这是我用于类似任务的内容-
# 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
}
The above will script out all the tables from source database to text files and then read the same from text files and create the tables on target database and target server. 上面的脚本将从源数据库到文本文件的所有表脚本化,然后从文本文件中读取它们,并在目标数据库和目标服务器上创建表。 Few things to keep in mind -
没什么要记住的-
dbo
, then you need to create those schemas first. dbo
之外还有其他任何模式,那么您需要首先创建这些模式。 Else your tables for other schemas won't be created. $true
or $false
for database objects as per your requirement. $true
或$false
。 You could have this 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"
Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
run after the tables are created or you can loop in the script after the create tables itself, so that table creation and loading of data into the tables happen simultaneously. Get-DbaTable -SqlInstance "Machine1" -Database DBA -Table "Table01" | Copy-DbaTableData -Destination "Machine2\\PANDA" -DestinationDatabase PANDA01 -DestinationTable "Table01"
在创建表后运行,或者您可以在创建表本身之后在脚本中循环执行,以便同时进行表创建和数据加载到表中。 That is entirely your choice and I leave it at your discretion. 这完全是您的选择,我将自行决定。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.