Retrieve Column Names as first row of query

  • Hi

    I am creating a query which exports data to a CSV file. The CSV export is done automatically from the recordset in ASP. I would like to have the first line of the CSV file be the field titles from the query.

    Is it possible in SQL to export the column/field titles as the first line of the result set?

    Thanks

    Simon

  • play with the text file (destination) object in dts or if you don't want to do that...

    select 'columnname' columnname

    union

    select columnname

    from MyTable

  • Thanks!

  • Simon,

    How do you export automatically?

    Maybe you can add a line to your VBscript in ASP page that exports recordset field names into comma-separeted string like 

         For i = 0 To rs.Fields.Count - 1

          MyString = MyString & "," & rs.Fields(i).Name 

       Next i

    and then you will write this string to a file as first line.

    Regards,Yelena Varsha

  • That's what I ended up using. The final VBscript code (off topic alert!) went something like:

    For fnum = 0 To rsRRS.Fields.Count-1

    strReturn = strReturn & rsRRS.Fields(fnum).Name & ","

    Next

    strReturn = strReturn & vbNewLine

    strReturn= strReturn & rsRRS.GetString (2, , ",",VbCrLf, "Null")

    rsRRS.Close

    set rsRRS = Nothing

    Response.AddHeader "Content-Disposition", "attachment; filename=file.txt"

    Response.Charset = "UTF-8"

    Response.ContentType = "application/octet-stream"

    Response.BinaryWrite strReturn

    Response.Flush

    Response.Clear

  • This is really cool how you export a file.

    Regards,Yelena Varsha

Viewing 6 posts - 1 through 5 (of 5 total)

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