Can we get columns names in the output file using bcp

  • Hi,

    Can we get the column names in the output file while using bcp utility. Pls advise.


    Kindest Regards,

    Saravanan V
    geocities.com

  • Syntax

    bcp {[[database_name.][owner].]{table_name | view_name} | "query"}

        {in | out | queryout | format} data_file

        [-m max_errors] [-f format_file] [-e err_file]

        [-F first_row] [-L last_row] [-b batch_size]

        [-n] [-c] [-w] [-N] [-V (60 | 65 | 70)] [-6]

        [-q] [-C code_page] [-t field_term] [-r row_term]

        [-i input_file] [-o output_file] [-a packet_size]

        [-S server_name[\instance_name]] [-U login_id] [-P password]

        [-T] [-v] [-R] [-k] [-E] [-h "hint [,...n]"]

    (Other Options)

    http://msdn2.microsoft.com/en-us/library/ms175915.aspx

    (BCP UTILITY)

    http://msdn2.microsoft.com/en-us/library/ms162802.aspx

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/coprompt/cp_bcp_61et.asp

    I don't see include field names as an option for BCP, so you have to do it yourself:

    You can create a "method for execution" using input variables

     which does two executes and uses some shared variables:

    1. First execute bcp on "table_name" "output_filename"

    2. Then execute select top 1 * from "table_name" to file "output_filename"2

    3. Then Trim & Merge the two together before sending the whole thing to its next destination

    PS: Select statment needs a little work to format fieldnames with delimeters, I would create a stored procedure for generating this using the (delimeter, table_name) as input and create one row of fields with sepperators as the output, guess this could be merged with BCP in SQL also, so no need to build it outside of SQL.

    Final: So try making one procedure which generates your complete file as input to BCP: this includes the procedure which creates the first row as (fields_names w/delimeters) union it to selection of table_name use it as one view in completing your task. When done this procedure should work with any table as input.

    BrainStorm: End result could be piped directly into an excel spreadsheet simply by using .cvs as the file extension. But then, we are not talking bulk data anymore?


    Regards,

    Coach James

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

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