-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathQueryResults-2-Excel.sql
35 lines (31 loc) · 1.4 KB
/
QueryResults-2-Excel.sql
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
/* 1) Execute sp_whoIsActive and store result in Excel */
$instance = 'ANN1VESPDB01';
$excelPath = "C:\Temp\$instance.xlsx";
$sqlQuery = @"
exec sp_whoIsActive @get_plans=1, @get_full_inner_text=1,
@get_transaction_info=1, @get_task_info=2,
@get_locks=1, @get_avg_time=1, @get_additional_info=1,
@find_block_leaders=1
"@;
Invoke-Sqlcmd -ServerInstance $instance -Query $sqlQuery | Export-Excel $excelPath -Show;
/* 2) Execute sp_whoIsActive and store result in Excel using ADO.NET Method*/
$instance = 'ANN1VESPDB01'
$excelPath = "C:\Temp\$instance.xlsx";
$sqlQuery = @"
exec sp_whoIsActive @get_plans=1, @get_full_inner_text=1,
@get_transaction_info=1, @get_task_info=2,
@get_locks=1, @get_avg_time=1, @get_additional_info=1,
@find_block_leaders=1
"@;
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$instance;Database=master;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $sqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandTimeout = 0
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet);
$SqlConnection.Close();
$DataSet.Tables[0] | Export-Excel $excelPath -Show;