commands

^

Copy-DbaDbViewData

Author Stephen Swan (@jaxnoth)
Availability Windows, Linux, macOS

 

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

Synopsis

Copies data from a SQL Server view to a table.

Description

Copies data from a SQL Server view to a table using SQL Bulk Copy.
With this function, a streaming copy will be done in the most speedy and least resource-intensive way.

Syntax

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

 

Examples

 

Example: 1
PS C:\> Copy-DbaDbViewData -SqlInstance sql1 -Destination sql2 -Database dbatools_from -View dbo.test_view

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

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

Copies all the data from view [Schema].[test view] (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-DbaDbView -SqlInstance sql1 -Database tempdb -View vw1, vw2 | Copy-DbaDbViewData -DestinationTable tb3

Copies all data from Views vw1 and vw2 in tempdb on sql1 to tb3 in tempdb on sql1

Example: 4
PS C:\> Get-DbaDbView -SqlInstance sql1 -Database tempdb -View vw1, vw2 | Copy-DbaDbViewData -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-DbaDbViewData -SqlInstance sql1 -Destination sql2 -Database dbatools_from -View test_view -KeepIdentity -Truncate

Copies all the data in view test_view 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'
>> View = '[Schema].[View]'
>> DestinationTable = '[dbo].[View.Copy]'
>> KeepIdentity = $true
>> KeepNulls = $true
>> Truncate = $true
>> BatchSize = 10000
>> }
>>
PS C:\> Copy-DbaDbViewData @params

Copies all the data from view [Schema].[View] 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'
>> View = '[AdventureWorks2017].[Person].[EmailPromotion]'
>> BatchSize = 10000
>> Query = "SELECT * FROM [OtherDb].[Person].[Person] where EmailPromotion = 1"
>> }
>>
PS C:\> Copy-DbaDbViewData @params

Copies data returned from the query on server1 into the AdventureWorks2017 on server1.
This query uses a 3-part name to reference the object in the query value, it will try to find the view named "Person" in the schema "Person" and database "OtherDb".
Copy is processed in BatchSize of 10000 rows. See the -Query param documentation for more details.

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
-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.

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 View

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
-InputObject

Enables piping of View objects from Get-DbaDbView

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