Vlad Drumea troubleshoots a common problem:
The code in this case is the GetStatsInfoForWholeDB.sql script that’s part of PSBlitz’s resources.
This script is used for, you wouldn’t believe by the name alone, getting statistics information for a specific database.Due to the fact that it might be ran on Azure or on older versions of SQL Server, as well as on databases with incremental statistics, the best option for it was to use dynamic SQL.
In this case it uses a variable
@SQL
defined as NVARCHAR(MAX) to store the query that’s built at runtime and execute it viaEXEC
.
Read on for one of the most common issues you may run into around generating dynamic SQL.