简体   繁体   English

DBATOOLS-从表中复制数据并在DestinationDatabase上创建

[英]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 - 没什么要记住的-

  1. The script requires the database to be present in the target server. 该脚本要求数据库存在于目标服务器中。 If not, the script fails. 如果不是,脚本将失败。 So create your target databases before executing this. 因此,在执行此操作之前,请创建目标数据库。
  2. If there are any other schemas in your database other than dbo , then you need to create those schemas first. 如果您的数据库中除了dbo之外还有其他任何模式,那么您需要首先创建这些模式。 Else your tables for other schemas won't be created. 否则,将不会为其他模式创建您的表。
  3. For the list of scripting options you could refer to this msdn link where you can set scripting options as $true or $false for database objects as per your requirement. 有关脚本选项的列表,您可以参考 msdn链接,您可以在其中根据需要将数据库对象的脚本选项设置为$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM