Excel Export Question

  • I have been using the script from link below to export an SQL query to an excel file which works great, but i have started to make this into a Stored procedure so i can give filename, and sql statement and produce the excel file.  

    How do I Create the Table and worksheet without knowing how many fieldsthere will be, as this will change depending on the SQL query.

    I want to bae able to send

    SELECT * FROM ....    ( I know your not supposed to use this but i write one off statements to extract data using query analyser, so select * would be good but named fields would be just as good)

    or

    SELECT name,address,phone FROM

    or

    SELECT name FROM

    there could be 20-30 fields (or more)

    without having to change the SP.

    http://www.sqlservercentral.com/scripts/contributions/763.asp

    THANKS

  • I guess if i can find a way of creating a table based on the SELECT statement passed to the SP, i can get it to work unless anyone has any better ideas.

    Thanks

  • You could use 3 input parameters for your Stored Procedure, @CreateTable varchar(1000), @InsertTable varchar(1000), @SelectTable varchar(2000)

    Then change the code like:

    SET @DDL = @CreateTable

    SET @sql = @InsertTable + ' ' + @SelectTable

    Andy

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply