Skip to content

Commit 9899bd2

Browse files
Copy-DbaDbTableData - Add -ScriptingOptionsObject parameter (#10317)
1 parent a38bc6b commit 9899bd2

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

public/Copy-DbaDbTableData.ps1

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ function Copy-DbaDbTableData {
127127
Creates new tables using the destination database's default filegroup instead of matching the source table's filegroup name.
128128
Use this when the destination database has different filegroup configurations or when you want all copied tables in the PRIMARY filegroup.
129129
130+
.PARAMETER ScriptingOptionsObject
131+
A scripting options object created by New-DbaScriptingOption that controls how the destination table is scripted when -AutoCreateTable is used.
132+
Use this to control which table properties are included in the CREATE TABLE script, such as indexes, constraints, triggers, and extended properties.
133+
When specified, this takes precedence over -UseDefaultFileGroup. Use New-DbaScriptingOption to create the object and set the desired properties.
134+
130135
.OUTPUTS
131136
PSCustomObject
132137
@@ -230,6 +235,15 @@ function Copy-DbaDbTableData {
230235
231236
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
232237
238+
.EXAMPLE
239+
PS C:\> $so = New-DbaScriptingOption
240+
PS C:\> $so.DriAll = $true
241+
PS C:\> $so.Indexes = $true
242+
PS C:\> $so.NoFileGroup = $true
243+
PS C:\> Copy-DbaDbTableData -SqlInstance sql1 -Destination sql2 -Database db1 -Table dbo.MyTable -AutoCreateTable -ScriptingOptionsObject $so
244+
245+
Copies all data from dbo.MyTable in db1 on sql1 to an auto-created table on sql2, scripting the destination table with all constraints, indexes, and using the default filegroup.
246+
233247
.EXAMPLE
234248
PS C:\> $params = @{
235249
>> SqlInstance = "SERVER001"
@@ -273,6 +287,7 @@ function Copy-DbaDbTableData {
273287
[int]$BulkCopyTimeout = 5000,
274288
[int]$CommandTimeout = 0,
275289
[switch]$UseDefaultFileGroup,
290+
[Microsoft.SqlServer.Management.Smo.ScriptingOptions]$ScriptingOptionsObject,
276291
[Parameter(ValueFromPipeline)]
277292
[Microsoft.SqlServer.Management.Smo.TableViewBase[]]$InputObject,
278293
[switch]$EnableException
@@ -293,12 +308,18 @@ function Copy-DbaDbTableData {
293308
}
294309
}
295310

296-
$defaultFGScriptingOption = @{
297-
ScriptingOptionsObject = $(
298-
$so = New-DbaScriptingOption
299-
$so.NoFileGroup = $UseDefaultFileGroup
300-
$so
301-
)
311+
if (Test-Bound -ParameterName ScriptingOptionsObject) {
312+
$defaultFGScriptingOption = @{
313+
ScriptingOptionsObject = $ScriptingOptionsObject
314+
}
315+
} else {
316+
$defaultFGScriptingOption = @{
317+
ScriptingOptionsObject = $(
318+
$so = New-DbaScriptingOption
319+
$so.NoFileGroup = $UseDefaultFileGroup
320+
$so
321+
)
322+
}
302323
}
303324
}
304325

tests/Copy-DbaDbTableData.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Describe $CommandName -Tag UnitTests {
3434
"BulkCopyTimeout",
3535
"CommandTimeout",
3636
"UseDefaultFileGroup",
37+
"ScriptingOptionsObject",
3738
"InputObject",
3839
"EnableException"
3940
)

0 commit comments

Comments
 (0)