This is not easy in SSIS, because you need a Data Conversion component between your table source and your Excel destination. That Data Conversion component must have different columns every time you have a query with different output columns.
On the web I found some solutions, but none of them worked for me. So I wrote my own solution, that basically consists of the following steps:
- Get the contents of an SQL script file (in that file you can put a select query).
- Create a temporary table from the script code (for determining the structure of the output).
- Generate and execute a script for creating the Excel worksheet.
- Dynamically generate a temporary package for executing the SQL script with an Excel file as output.
- Execute and drop the dynamic package.
Currently I am writing an article on www.codeproject.com to share what I have developed. However, it may take a while until it is finshed.