Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created December 6, 2017 09:05
Show Gist options
  • Save ghotz/13726ced84c945d354e3a5cd80dfbcd9 to your computer and use it in GitHub Desktop.
Save ghotz/13726ced84c945d354e3a5cd80dfbcd9 to your computer and use it in GitHub Desktop.
Execute query with SqlCmd variables and save SQL Server execution plan example
$SqlCmdVariables = "DatabaseName=master", "ColumnName=name";
$Results = (Invoke-Sqlcmd -ServerInstance "localhost\sql2017" -Query "SET STATISTICS XML ON; SELECT `$(ColumnName) FROM sys.databases WHERE [name] = '`$(DataBaseName)';" -Variable $SqlCmdVariables -MaxCharLength 10MB);
$Plan = [xml]$Results[$Results.GetUpperBound(0)].Item(0);
$Plan.Save("C:\temp\test.sqlplan");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment