SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Mechanism To Dynamically Build Pivot Table

By Gaby Abed,

This code provides a mechanism to generate dynamic pivot tables.  In this case, we're checking backup history to X months, checking average backups each month.
There is a @basequery that can be anything, so long as you place two tags for columns and list, surrounded by << >>.
In this example, if you change @numMonths, you will see those values evolve in the loop if you uncomment the DEBUG: lines.

Total article views: 487 | Views in the last 30 days: 50
dynamic pivot    
pivot table