Repository navigation
Add the -ScriptingOptionsObject parameter to Export-DbaDbTableData for parity with Export-DbaScript. #10778
Description
Activity
- addedtriage requiredNew issue that has not been reviewed by maintainersNew issue that has not been reviewed by maintainers
on Oct 8, 2026 Thanks for the request, @charring. I checked this against the current
developmentcode and in a lab against SQL Server 2019.Why the
USEis always thereExport-DbaDbTableDatais a thin wrapper aroundExport-DbaScript. It builds its own scripting options withIncludeDatabaseContext = $truehard-coded (Export-DbaDbTableData.ps1#L128-L131), and you cannot override them.-NoPrefixonly drops the/* Created by ... */comment header, exactly as it does inExport-DbaScript. The help for-NoPrefixsays it also removes theUSEstatement. That is wrong, and it explains why you expected it to work. The wrong sentence came in with a help rewrite in #9810.Workaround until this is fixed
Call
Export-DbaScriptdirectly with your own options. That is allExport-DbaDbTableDatadoes internally:$options = New-DbaScriptingOption $options.ScriptSchema = $false $options.ScriptData = $true $options.IncludeDatabaseContext = $false Get-DbaDbTable -SqlInstance sql01 -Database MyDb -Table dbo.Table1, dbo.Table2 | Export-DbaScript -ScriptingOptionsObject $options -FilePath C:\temp\data.sql -NoPrefix
This writes the
INSERTstatements of both tables into one file, with noUSEand aGOafter each statement. Add-BatchSeparator ""if you do not want theGO.Proposal
I agree that
-ScriptingOptionsObjectis the better fix, and that-NoPrefixshould stay as it is. InExport-DbaScriptandExport-DbaSysDbUserObject,-NoPrefixmeans "no comment header". Making it also drop theUSEhere would silently change the scripts of everyone who uses it today. The new parameter would work as it does inExport-DbaDbRoleandExport-DbaServerRole: without it you get the current defaults, and an object you pass is used exactly as given. So you setScriptData = $trueandScriptSchema = $falseyourself, as in the example above, or keepScriptSchema = $trueif you want the table definition as well.Two more problems I found while testing
- If you pipe several tables into
Export-DbaDbTableData -FilePath x.sqlwithout-Append, only the last table ends up in the file. Each table overwrites the one before, because the command callsExport-DbaScriptonce per pipeline object. This is the problem reported in export-dbaDbTableData generates files with same timestamp #9044. That issue was closed by changing the examples to use-Append, but the behaviour itself was never fixed. -BatchSeparatoris declared with a default of"", but that default is never passed on toExport-DbaScript. Files therefore get theGOfrom theFormatting.BatchSeparatorsetting. The remark in the second example that the script "does not include Batch Separator" is wrong for the same reason.
I will open one pull request for
Export-DbaDbTableDatathat fixes all of this.
This text was created by Claude and reviewed by Andreas Jordan.
- If you pipe several tables into
- added and removedtriage requiredNew issue that has not been reviewed by maintainersNew issue that has not been reviewed by maintainers
on Oct 11, 2026
Summarize Functionality
I want to be able to export data but not have a USE statement at the start.
Another solution might be that the NoPrefix flag would also stop the USE statement from being added.
But as there's already a IncludeDatabaseContext flag in DbaScriptingOption, it's probably better to add the ScriptingOptionsObject parameter support.
Is there a command that is similiar or close to what you are looking for?
Yes
Technical Details
No response