-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.ExcelRoundTrip.ps1
More file actions
151 lines (133 loc) · 5.07 KB
/
Copy pathExample.ExcelRoundTrip.ps1
File metadata and controls
151 lines (133 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
param(
[string] $Server = $(if ($env:DBACLIENTX_SQLSERVER) { $env:DBACLIENTX_SQLSERVER } else { 'localhost' }),
[string] $Database = $(if ($env:DBACLIENTX_SQLDATABASE) { $env:DBACLIENTX_SQLDATABASE } else { 'tempdb' }),
[string] $SourceTable = 'dbo.DbaClientXExcelSource',
[string] $DestinationTable = 'dbo.DbaClientXExcelRoundTrip',
[string] $ExcelPath = (Join-Path $PWD 'DbaClientXExcelRoundTrip.xlsx'),
[string] $WorksheetName = 'Rows',
[int] $RowCount = 100,
[int] $QueryTimeout = 120,
[switch] $KeepArtifacts
)
# Use this example to prove the SQL Server -> Excel -> SQL Server workflow:
# DbaClientX reads source rows, PSWriteOffice writes and reads the workbook,
# and DbaClientX streams the imported Excel reader back to SQL Server.
# Example:
# .\Example.ExcelRoundTrip.ps1 -Server localhost -Database tempdb -RowCount 100 -KeepArtifacts
Import-Module DbaClientX -Force -ErrorAction Stop
Import-Module PSWriteOffice -Force -ErrorAction Stop
if ($RowCount -lt 1) {
throw 'RowCount must be greater than zero.'
}
if ($QueryTimeout -lt 0) {
throw 'QueryTimeout cannot be negative.'
}
$connectionString = "Server=$Server;Database=$Database;Encrypt=True;TrustServerCertificate=True;Integrated Security=True"
Invoke-DbaXNonQuery -Server $Server -Database $Database -TrustServerCertificate -Query @"
IF OBJECT_ID(N'$SourceTable', N'U') IS NOT NULL DROP TABLE $SourceTable;
IF OBJECT_ID(N'$DestinationTable', N'U') IS NOT NULL DROP TABLE $DestinationTable;
CREATE TABLE $SourceTable
(
Id int NOT NULL,
DisplayName nvarchar(100) NOT NULL,
Score decimal(18,2) NOT NULL,
CreatedUtc datetime2 NOT NULL
);
WITH numbers AS
(
SELECT TOP ($RowCount)
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Id
FROM sys.all_objects AS a
CROSS JOIN sys.all_objects AS b
)
INSERT INTO $SourceTable (Id, DisplayName, Score, CreatedUtc)
SELECT
Id,
CONCAT(N'Row ', Id),
CONVERT(decimal(18,2), Id * 1.25),
SYSUTCDATETIME()
FROM numbers;
"@ -ErrorAction Stop | Out-Null
try {
if (Test-Path -LiteralPath $ExcelPath) {
Remove-Item -LiteralPath $ExcelPath -Force
}
$client = [DBAClientX.SqlServer]::new()
$sqlReader = $null
try {
$client.CommandTimeout = $QueryTimeout
$sqlReader = $client.QueryReader($connectionString, "SELECT Id, DisplayName, Score, CreatedUtc FROM $SourceTable ORDER BY Id;")
Export-OfficeExcel `
-InputObject $sqlReader `
-Path $ExcelPath `
-WorksheetName $WorksheetName `
-TableName 'DbaClientXRows' `
-AutoFit `
-ErrorAction Stop | Out-Null
} finally {
if ($null -ne $sqlReader) {
$sqlReader.Dispose()
}
$client.Dispose()
}
$importedExcel = Import-OfficeExcel `
-Path $ExcelPath `
-WorksheetName $WorksheetName `
-AsDataReader `
-ErrorAction Stop
try {
$inputObject = if ($importedExcel -is [System.Data.IDataReader]) { (, $importedExcel) } else { $importedExcel }
$writeResult = Write-DbaXTableData `
-Provider SqlServer `
-ConnectionString $connectionString `
-DestinationTable $DestinationTable `
-InputObject $inputObject `
-AutoCreateTable `
-TableLock `
-BatchSize 5000 `
-PassThru `
-ErrorAction Stop
} finally {
if ($importedExcel -is [System.IDisposable]) {
$importedExcel.Dispose()
}
}
$verification = Invoke-DbaXQuery `
-Server $Server `
-Database $Database `
-TrustServerCertificate `
-Query "SELECT COUNT(*) AS SourceRows FROM $SourceTable; SELECT COUNT(*) AS DestinationRows FROM $DestinationTable;" `
-ReturnType DataSet `
-ErrorAction Stop
$sourceRows = [int] $verification.Tables[0].Rows[0]['SourceRows']
$destinationRows = [int] $verification.Tables[1].Rows[0]['DestinationRows']
$hasMismatch = $writeResult.Rows -ne $RowCount -or
$sourceRows -ne $RowCount -or
$destinationRows -ne $RowCount
if ($hasMismatch) {
throw "Round-trip row count mismatch. Written=$($writeResult.Rows), Source=$sourceRows, Destination=$destinationRows, Expected=$RowCount."
}
[pscustomobject]@{
Server = $Server
Database = $Database
ExcelPath = $ExcelPath
SourceTable = $SourceTable
DestinationTable = $DestinationTable
ExportedRows = $RowCount
ImportedRows = $writeResult.Rows
WrittenRows = $writeResult.Rows
SourceRows = $sourceRows
DestinationRows = $destinationRows
}
}
finally {
if (-not $KeepArtifacts) {
Invoke-DbaXNonQuery -Server $Server -Database $Database -TrustServerCertificate -Query @"
IF OBJECT_ID(N'$DestinationTable', N'U') IS NOT NULL DROP TABLE $DestinationTable;
IF OBJECT_ID(N'$SourceTable', N'U') IS NOT NULL DROP TABLE $SourceTable;
"@ -ErrorAction SilentlyContinue | Out-Null
if (Test-Path -LiteralPath $ExcelPath) {
Remove-Item -LiteralPath $ExcelPath -Force
}
}
}