-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-QueryPlan.ps1
153 lines (133 loc) · 4.11 KB
/
Get-QueryPlan.ps1
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
152
153
<#
.Synopsis
Formatting your T-SQL code
.DESCRIPTION
Formatting T-SQL code through RedGate Format Api (https://www.red-gate.com/products/sql-development/sql-prompt/)
Works on PowerShell Core (aka PowerShell 6+)
.EXAMPLE
$Script = '--(Query 16)_(AlwaysOn AG Cluster)
SELECT cluster_name, quorum_type_desc, quorum_state_desc
FROM sys.dm_hadr_cluster WITH (NOLOCK) OPTION (RECOMPILE);
------'
Format-SQLCode -Script $Script -Style Default
.EXAMPLE
Formatting one file
Format-SQLCode `
-FullName 'C:\SQL Server 2014 Diagnostic Information Queries\(Query 11)_(SQL Server Agent Alerts).sql' `
-Style Default
.EXAMPLE
Formatting all file on a directory
Get-ChildItem 'C:\SQL Server 2014 Diagnostic Information Queries' -File | Format-SQLCode -Style Default
.EXAMPLE
Formatting all file on a directory and save the result to file.
$ListFiles = Get-ChildItem 'C:\Temp\SQL Server 2014 Diagnostic Information Queries\' -File
Foreach($File in $ListFiles) {
$File | Format-SQLCode -Style Indented | Set-Content -PassThru -Path $File.FullName
}
.LINK
Author: Mateusz Nadobnik
Link: http://mnadobnik.pl/format-sqlcode
Date: 01.02.2019
Version: 1.0.0.0
Keywords: Formatting, T-SQL, RedGate, SQL Prompt
Notes:
Changelog:
#>
######################################################################################
#
# File Name: Get-QueryPlan.ps1
#
# Applies to: SQL Server 2008
# SQL Server 2008 R2
# SQL Server 2012
#
# Purpose: Used to retrieve an XML query plan from cache.
#
# Prerequisite: Powershell must be installed.
# SQL Server components must be installed.
#
# Parameters: [string]$SqlInstance - SQL Server name (Ex: SERVER\INSTANCE)
# [string]$PlanHandle - Binary query handle
#
# Author: Patrick Keisler
#
# Version: 1.0.0
#
# Date: 08/30/2013
#
# Help: http://www.patrickkeisler.com/2013/09/the-case-of-null-queryplan.html
#
######################################################################################
#Define input parameters
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$SqlInstance
,[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$PlanHandle
)
Write-Host "Script starting."
#Grab the path where the Powershell script was executed from.
$path = Split-Path $MyInvocation.MyCommand.Path
#Build the SQL Server connection objects
$conn = New-Object System.Data.SqlClient.SqlConnection
$builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder
$cmd = New-Object System.Data.SqlClient.SqlCommand
#Build the TSQL statement & connection string
$SqlCommand = "SELECT query_plan FROM sys.dm_exec_text_query_plan(" + $PlanHandle + ",DEFAULT,DEFAULT);"
$builder.psBase.DataSource = $SqlInstance
$builder.psBase.InitialCatalog = "master"
$builder.psBase.IntegratedSecurity = $true
$builder.psBase.ApplicationName = "Get-QueryPlan"
$builder.psBase.Pooling = $true
$builder.psBase.ConnectTimeout = 15
$conn.ConnectionString = $builder.ConnectionString
$cmd.Connection = $conn
$cmd.CommandText = $SqlCommand
try
{
if ($conn.State -eq "Closed")
{
#Open a connection to SQL Server
$conn.Open()
}
#Execute the TSQL statement
[string]$QueryPlanText = $cmd.ExecuteScalar()
#Write the output to a file
$FileName = $path + "\output.sqlplan"
$stream = New-Object System.IO.StreamWriter($FileName)
$stream.WriteLine($QueryPlanText)
if ($stream.BaseStream -ne $null)
{
#Close the stream object
$stream.close()
}
if ($conn.State -eq "Open")
{
#Close the SQL Server connection
$conn.Close()
}
Write-Host "Script completed successfully."
}
catch
{
#Capture errors if needed
if ($_.Exception.InnerException)
{
$Host.UI.WriteErrorLine("ERROR: " + $_.Exception.InnerException.Message)
if ($_.Exception.InnerException.InnerException)
{
$Host.UI.WriteErrorLine("ERROR: " + $_.Exception.InnerException.InnerException.Message)
}
}
else
{
$Host.UI.WriteErrorLine("ERROR: " + $_.Exception.Message)
}
Write-Host .
Write-Host "ERROR: Script failed."
}