While you can use EXECUTE
] to run a dynamic SQL statement, I'm really suspicious if this is a good approach.
If you dynamically define the result set columns wouldn't this result into a situation where you have difficulties in selecting the values from the cursor since you don't know the columns?
Dynamic sql could be feasible when you have changing conditions, but I would consider other kind of approaches for this.
Added a small example: Calculate aggregates for dynamic columns using UNPIVOT