< Back
Copy-DbaDbTableData
Post
NAME Copy-DbaDbTableData
SYNOPSIS
Copies data between SQL Server tables.
SYNTAX
Copy-DbaDbTableData [[-SqlInstance] <DbaInstanceParameter>] [[-SqlCredential] <Pscredential>] [[-Destination]
<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-DestinationSqlCredential] <Pscredential>]
[[-Database] <String>] [[-DestinationDatabase] <String>] [[-Table] <System.String[]>] [[-Query] <String>]
[-AutoCreateTable <Switch>] [[-BatchSize] <Int>] [[-NotifyAfter] <Int>] [[-DestinationTable] <String>]
[-NoTableLock <Switch>] [-CheckConstraints <Switch>] [-FireTriggers <Switch>] [-KeepIdentity <Switch>] [-KeepNulls
<Switch>] [-Truncate <Switch>] [[-bulkCopyTimeOut] <Int>] [[-InputObject]
<Microsoft.SqlServer.Management.Smo.Table[]>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Copies data between SQL Server tables using SQL Bulk Copy.
The same can be achieved also doing
$sourcetable = Invoke-DbaQuery -SqlInstance instance1 ... -As DataTable
Write-DbaDbTableData -SqlInstance ... -InputObject $sourcetable
but it will force buffering the contents on the table in memory (high RAM usage for large tables).
With this function, a streaming copy will be done in the most speedy and least resource-intensive way.
PARAMETERS
-AutoCreateTable [<Switch>]
Creates the destination table if it does not already exist, based off of the "Export..." script of the source
table.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSize [<Int>]
The BatchSize for the import defaults to 5000.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-bulkCopyTimeOut [<Int>]
Value in seconds for the BulkCopy operations timeout. The default is 30 seconds.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-CheckConstraints [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<String>]
The database to copy the table from.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Destination [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
Destination Sql Server. You must have sysadmin access and server version must be SQL Server version 2000 or
greater.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationDatabase [<String>]
The database to copy the table to. If not specified, it is assumed to be the same of Database
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationSqlCredential [<Pscredential>]
Login to the target 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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationTable [<String>]
The table you want to use as destination. If not specified, it is assumed to be the same of Table
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FireTriggers [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<Microsoft.SqlServer.Management.Smo.Table[]>]
Enables piping of Table objects from Get-DbaDbTable
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-KeepIdentity [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-KeepNulls [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoTableLock [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NotifyAfter [<Int>]
Sets the option to show the notification after so many rows of import
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Query [<String>]
If you want to copy only a portion of a table or selected tables, specify the query.
Ensure to select all required columns. Calculated Columns or columns with default values may be excluded.
The tablename should be a full three-part name in form [Database].[Schema].
SYNOPSIS
Copies data between SQL Server tables.
SYNTAX
Copy-DbaDbTableData [[-SqlInstance] <DbaInstanceParameter>] [[-SqlCredential] <Pscredential>] [[-Destination]
<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-DestinationSqlCredential] <Pscredential>]
[[-Database] <String>] [[-DestinationDatabase] <String>] [[-Table] <System.String[]>] [[-Query] <String>]
[-AutoCreateTable <Switch>] [[-BatchSize] <Int>] [[-NotifyAfter] <Int>] [[-DestinationTable] <String>]
[-NoTableLock <Switch>] [-CheckConstraints <Switch>] [-FireTriggers <Switch>] [-KeepIdentity <Switch>] [-KeepNulls
<Switch>] [-Truncate <Switch>] [[-bulkCopyTimeOut] <Int>] [[-InputObject]
<Microsoft.SqlServer.Management.Smo.Table[]>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Copies data between SQL Server tables using SQL Bulk Copy.
The same can be achieved also doing
$sourcetable = Invoke-DbaQuery -SqlInstance instance1 ... -As DataTable
Write-DbaDbTableData -SqlInstance ... -InputObject $sourcetable
but it will force buffering the contents on the table in memory (high RAM usage for large tables).
With this function, a streaming copy will be done in the most speedy and least resource-intensive way.
PARAMETERS
-AutoCreateTable [<Switch>]
Creates the destination table if it does not already exist, based off of the "Export..." script of the source
table.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSize [<Int>]
The BatchSize for the import defaults to 5000.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-bulkCopyTimeOut [<Int>]
Value in seconds for the BulkCopy operations timeout. The default is 30 seconds.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-CheckConstraints [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<String>]
The database to copy the table from.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Destination [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
Destination Sql Server. You must have sysadmin access and server version must be SQL Server version 2000 or
greater.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationDatabase [<String>]
The database to copy the table to. If not specified, it is assumed to be the same of Database
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationSqlCredential [<Pscredential>]
Login to the target 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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationTable [<String>]
The table you want to use as destination. If not specified, it is assumed to be the same of Table
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FireTriggers [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<Microsoft.SqlServer.Management.Smo.Table[]>]
Enables piping of Table objects from Get-DbaDbTable
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-KeepIdentity [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-KeepNulls [<Switch>]
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."
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoTableLock [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NotifyAfter [<Int>]
Sets the option to show the notification after so many rows of import
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Query [<String>]
If you want to copy only a portion of a table or selected tables, specify the query.
Ensure to select all required columns. Calculated Columns or columns with default values may be excluded.
The tablename should be a full three-part name in form [Database].[Schema].
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<Pscredential>]
Login to the target 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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<DbaInstanceParameter>]
Source SQL Server.You must have sysadmin access and server version must be SQL Server version 2000 or greater.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Table [<System.String[]>]
Define a specific table you would like to use as source. You can specify a three-part name like db.sch.tbl.
If the object has special characters please wrap them in square brackets [ ].
This dbo.First.Table will try to find table named 'Table' on schema 'First' and database 'dbo'.
The correct way to find table named 'First.Table' on schema 'dbo' is passing dbo.[First.Table]
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Truncate [<Switch>]
If this switch is enabled, the destination table will be truncated after prompting for confirmation.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
[b]INPUTS[/b]
[b]OUTPUTS[/b]
[b]NOTES[/b]
Tags: Migration
Author: Simone Bizzotto (@niphlod)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- 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 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] 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 tbl1 in tempdb on sql1 to tbl1 in tempdb on sql2
then
Copies data from tbl2 in tempdb on sql1 to tbl2 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] 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 = '[Person].[EmailPromotion]'
>> BatchSize = 10000
>> Query = "SELECT * FROM [Person].[Person] where EmailPromotion = 1"
>> }
>>
PS C:\\> Copy-DbaDbTableData @params
Copies data returned from the query on server1 into the AdventureWorks2017 on server1.
Copy is processed in BatchSize of 10000 rows. Presuming the Person.EmailPromotion exists already.
[b]RELATED LINKS[/b]
https://dbatools.io/Copy-DbaDbTableData