You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d5da4b9
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: public/Export-DbaDbTableData.ps1
+63-13Lines changed: 63 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,19 @@ function Export-DbaDbTableData {
4
4
Generates INSERT statements from table data for migration and deployment scripts
5
5
6
6
.DESCRIPTION
7
-
Creates executable INSERT statements from existing table data, making it easy to move data between SQL Server instances or environments. This is particularly useful for migrating reference tables, lookup data, or configuration tables where you need the actual data values rather than just the table structure. The generated scripts include proper USE database context and can be saved to files or piped to other commands for further processing.
7
+
Creates executable INSERT statements from existing table data, making it easy to move data between SQL Server instances or environments. This is particularly useful for migrating reference tables, lookup data, or configuration tables where you need the actual data values rather than just the table structure. By default the generated scripts start with a USE statement for the database of each table. They can be saved to files or piped to other commands for further processing.
8
+
9
+
Tables piped into one call that write to the same file all end up in that file, even without -Append.
8
10
9
11
.PARAMETERInputObject
10
12
Accepts table objects from Get-DbaDbTable through the pipeline.
11
13
Use this to process specific tables you've already identified rather than specifying table names again.
12
14
15
+
.PARAMETERScriptingOptionsObject
16
+
Accepts an SMO ScriptingOptions object created with New-DbaScriptingOption to control script generation.
17
+
Without it, the command scripts the data only (ScriptData = $true, ScriptSchema = $false) and includes the USE statement (IncludeDatabaseContext = $true).
18
+
An object you pass is used exactly as given, so set ScriptData = $true yourself. Set IncludeDatabaseContext = $false to omit the USE statement, or keep ScriptSchema = $true to script the table definition as well.
19
+
13
20
.PARAMETERPath
14
21
Sets the directory where output files will be created when not using FilePath.
15
22
Defaults to the dbatools export directory configured in module settings.
@@ -37,12 +44,12 @@ function Export-DbaDbTableData {
37
44
Useful for reviewing the script content before execution or when piping to other commands.
38
45
39
46
.PARAMETERBatchSeparator
40
-
Adds batch separators (like GO) between INSERT statements in the output script.
41
-
Use this when creating deployment scripts that will be executed in SQL Server Management Studio or sqlcmd.
47
+
Adds a batch separator (like GO) after every statement in the output file, and in the output of -Passthru.
48
+
Defaults to the Formatting.BatchSeparator configuration value, which is GO. Pass an empty string to write the statements without a separator.
42
49
43
50
.PARAMETERNoPrefix
44
-
Excludes the USE database statement and other prefixes from the generated script.
45
-
Use this when combining output with other scripts or when the database context is already established.
51
+
Excludes the comment header with the creator, timestamp, and dbatools version from the generated script.
52
+
This does not remove the USE statement. To omit it, pass a ScriptingOptionsObject with IncludeDatabaseContext set to $false.
46
53
47
54
.PARAMETERNoClobber
48
55
Prevents overwriting existing files at the specified FilePath.
@@ -89,7 +96,7 @@ function Export-DbaDbTableData {
89
96
90
97
System.String (when -Passthru is specified)
91
98
92
-
Returns the generated INSERT statements as string output. Multiple strings are returned for table data scripts, one per INSERT statement or batch. Use -BatchSeparator parameter to control statement separation with GO or other batch terminators.
99
+
Returns the generated INSERT statements as string output. Multiple strings are returned for table data scripts, one per INSERT statement or batch. Each one ends with the batch separator of -BatchSeparator, as in the file.
Exports data from EmployeePayHistory in AdventureWorks2014 in sql2017 using a trusted connection - Will append the output to the file C:\temp\export.sql if it already exists
103
-
Script does not include Batch Separator and will not compile
110
+
Every statement is followed by the batch separator from the Formatting.BatchSeparator configuration value, which is GO
Exports the data of both tables to C:\temp\export.sql without the USE statement and without the comment header, so the script runs in whatever database it is executed in
Copy file name to clipboardExpand all lines: public/Export-DbaScript.ps1
+23-4Lines changed: 23 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ function Export-DbaScript {
25
25
Controls the character encoding used when writing script files to disk.
26
26
Default is UTF8 which handles international characters and is widely supported.
27
27
Use UTF8 for most scenarios, or ASCII if you need compatibility with older tools that don't support Unicode.
28
+
This also applies to the statements SMO writes to the file when the ScriptingOptionsObject has ScriptBatchTerminator set, so the Encoding and AnsiFile settings of the ScriptingOptionsObject are not used for files.
28
29
29
30
.PARAMETERPassthru
30
31
Returns the generated T-SQL script as string output instead of writing to a file.
@@ -34,7 +35,7 @@ function Export-DbaScript {
34
35
.PARAMETERScriptingOptionsObject
35
36
Accepts a customized SMO ScriptingOptions object created with New-DbaScriptingOption to control script generation.
36
37
Use this when you need specific formatting like including schema context, headers, or data along with object definitions.
37
-
Settings in this object override other Export-DbaScript parameters like BatchSeparator.
38
+
BatchSeparator still separates the statements, except in a file written with ScriptBatchTerminator set, where SMO writes its own batch terminator.
38
39
39
40
.PARAMETERBatchSeparator
40
41
Sets the batch terminator added between SQL statements in the output script.
0 commit comments