commands

^

Copy-DbaDbTableData

Author Simone Bizzotto (@niphlod)
Availability Windows, Linux, macOS

 

Synopsis

Copies data between SQL Server tables.

Description

Copies data between SQL Server tables using SQL Bulk Copy. The same can be achieved also using Invoke-DbaQuery and Write-DbaDbTableData but it will buffer the contents of that table in memory of the machine running the commands. This function prevents that by streaming a copy of the data in the most speedy and least resource-intensive way.

Syntax

Copy-DbaDbTableData
    [[-SqlInstance] <DbaInstanceParameter>]
    [[-SqlCredential] <PSCredential>]
    [[-Destination] <DbaInstanceParameter[]>]
    [[-DestinationSqlCredential] <PSCredential>]
    [[-Database] <String>]
    [[-DestinationDatabase] <String>]
    [[-Table] <String[]>]
    [[-View] <String[]>]
    [[-Query] <String>]
    [-AutoCreateTable]
    [[-BatchSize] <Int32>]
    [[-NotifyAfter] <Int32>]
    [[-DestinationTable] <String>]
    [-NoTableLock]
    [-CheckConstraints]
    [-FireTriggers]
    [-KeepIdentity]
    [-KeepNulls]
    [-Truncate]
    [[-BulkCopyTimeout] <Int32>]
    [[-CommandTimeout] <Int32>]
    [[-InputObject] <TableViewBase[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Copy-DbaDbTableData -SqlInstance sql1 -Destination sql2 -Database dbatools_from -Table dbo.test_table

Copies all the data from table dbo.test_table (2-part name) in database dbatools_from on sql1 to table test_table in database dbatools_from on sql2.

Example: 2
PS C:\> Copy-DbaDbTableData -SqlInstance sql1 -Destination sql2 -Database dbatools_from -DestinationDatabase dbatools_dest -Table [Schema].[test table]

Copies all the data from table [Schema].[test table] (2-part name) in database dbatools_from on sql1 to table [Schema].[test table] in database dbatools_dest on sql2

Example: 3
PS C:\> Get-DbaDbTable -SqlInstance sql1 -Database tempdb -Table tb1, tb2 | Copy-DbaDbTableData -DestinationTable tb3

Copies all data from tables tb1 and tb2 in tempdb on sql1 to tb3 in tempdb on sql1

Example: 4
PS C:\> Get-DbaDbTable -SqlInstance sql1 -Database tempdb -Table tb1, tb2 | Copy-DbaDbTableData -Destination sql2

Copies data from tb1 and tb2 in tempdb on sql1 to the same table in tempdb on sql2

Example: 5
PS C:\> Copy-DbaDbTableData -SqlInstance sql1 -Destination sql2 -Database dbatools_from -Table test_table -KeepIdentity -Truncate

Copies all the data in table test_table from sql1 to sql2, using the database dbatools_from, keeping identity columns and truncating the destination

Example: 6
PS C:\> $params = @{
>> SqlInstance = 'sql1'
>> Destination = 'sql2'
>> Database = 'dbatools_from'
>> DestinationDatabase = 'dbatools_dest'
>> Table = '[Schema].[Table]'
>> DestinationTable = '[dbo].[Table.Copy]'
>> KeepIdentity = $true
>> KeepNulls = $true
>> Truncate = $true
>> BatchSize = 10000
>> }
>>
PS C:\> Copy-DbaDbTableData @params

Copies all the data from table [Schema].[Table] (2-part name) in database dbatools_from on sql1 to table [dbo].[Table.Copy] in database dbatools_dest on sql2
Keeps identity columns and Nulls, truncates the destination and processes in BatchSize of 10000.

Example: 7
PS C:\> $params = @{
>> SqlInstance = 'server1'
>> Destination = 'server1'
>> Database = 'AdventureWorks2017'
>> DestinationDatabase = 'AdventureWorks2017'
>> Table = '[AdventureWorks2017].[Person].[EmailPromotion]'
>> BatchSize = 10000
>> Query = "SELECT * FROM [OtherDb].[Person].[Person] where EmailPromotion = 1"
>> }
>>
PS C:\> Copy-DbaDbTableData @params

Copies data returned from the query on server1 into the AdventureWorks2017 on server1, using a 4-part name for the -Table parameter.
See the -Query param documentation for more details.
Copy is processed in BatchSize of 10000 rows.

Example: 8
PS C:\> Copy-DbaDbTableData -SqlInstance sql1 -Database tempdb -View [tempdb].[dbo].[vw1] -DestinationTable [SampleDb].[SampleSchema].[SampleTable] -AutoCreateTable

Copies all data from [tempdb].[dbo].[vw1] (3-part name) view on instance sql1 to an auto-created table [SampleDb].[SampleSchema].[SampleTable] on instance sql1

Optional Parameters

-SqlInstance

Source SQL Server.You must have sysadmin access and server version must be SQL Server version 2000 or greater.

Alias
Required False
Pipeline false
Default Value
-SqlCredential

Login to the source instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Destination

Destination Sql Server. You must have sysadmin access and server version must be SQL Server version 2000 or greater.

Alias
Required False
Pipeline false
Default Value
-DestinationSqlCredential

Login to the destination instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Database

The database to copy the table from.

Alias
Required False
Pipeline false
Default Value
-DestinationDatabase

The database to copy the table to. If not specified, it is assumed to be the same of Database

Alias
Required False
Pipeline false
Default Value
-Table

Specify a table to use as a source. You can specify a 2 or 3 part name. If the object has special characters please wrap them in square brackets. Note: Cannot specify a view if a table value is provided

Alias
Required False
Pipeline false
Default Value
-View

Specify a view to use as a source. You can specify a 2 or 3 part name (see examples). If the object has special characters please wrap them in square brackets. Note: Cannot specify a table if a view value is provided

Alias
Required False
Pipeline false
Default Value
-Query

Define a query to use as a source. Note: 3 or 4 part object names may be used (see examples) Ensure to select all required columns. Calculated Columns or columns with default values may be excluded. Note: The workflow in the command requires that a valid -Table or -View parameter value be specified.

Alias
Required False
Pipeline false
Default Value
-AutoCreateTable

Creates the destination table if it does not already exist, based off of the "Export..." script of the source table.

Alias
Required False
Pipeline false
Default Value False
-BatchSize

The BatchSize for the import defaults to 50000.

Alias
Required False
Pipeline false
Default Value 50000
-NotifyAfter

Sets the option to show the notification after so many rows of import. The default is 5000 rows.

Alias
Required False
Pipeline false
Default Value 5000
-DestinationTable

The table you want to use as destination. If not specified, it is assumed to be the same of Table

Alias
Required False
Pipeline false
Default Value
-NoTableLock

If this switch is enabled, a table lock (TABLOCK) will not be placed on the destination table. By default, this operation will lock the destination table while running.

Alias
Required False
Pipeline false
Default Value False
-CheckConstraints

If this switch is enabled, the SqlBulkCopy option to process check constraints will be enabled. Per Microsoft "Check constraints while data is being inserted. By default, constraints are not checked."

Alias
Required False
Pipeline false
Default Value False
-FireTriggers

If this switch is enabled, the SqlBulkCopy option to fire insert triggers will be enabled. Per Microsoft "When specified, cause the server to fire the insert triggers for the rows being inserted into the Database."

Alias
Required False
Pipeline false
Default Value False
-KeepIdentity

If this switch is enabled, the SqlBulkCopy option to preserve source identity values will be enabled. Per Microsoft "Preserve source identity values. When not specified, identity values are assigned by the destination."

Alias
Required False
Pipeline false
Default Value False
-KeepNulls

If this switch is enabled, the SqlBulkCopy option to preserve NULL values will be enabled. Per Microsoft "Preserve null values in the destination table regardless of the settings for default values. When not specified, null values are replaced by default values where applicable."

Alias
Required False
Pipeline false
Default Value False
-Truncate

If this switch is enabled, the destination table will be truncated after prompting for confirmation.

Alias
Required False
Pipeline false
Default Value False
-BulkCopyTimeout

Value in seconds for the BulkCopy operations timeout. The default is 5000 seconds.

Alias
Required False
Pipeline false
Default Value 5000
-CommandTimeout

Gets or sets the wait time (in seconds) before terminating the attempt to execute the reader and bulk copy operation. The default is 0 seconds (no timeout).

Alias
Required False
Pipeline false
Default Value 0
-InputObject

Enables piping of Table objects from Get-DbaDbTable

Alias
Required False
Pipeline true (ByValue)
Default Value
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

Alias cf
Required False
Pipeline false
Default Value

 

Want to see the source code for this command? Check out Copy-DbaDbTableData on GitHub.
Want to see the Bill Of Health for this command? Check out Copy-DbaDbTableData.