Getting column names with BCP queryout

前端 未结 1 1588
礼貌的吻别
礼貌的吻别 2021-01-06 01:48

I need to BCP a table into a tab-delimited file, but I need the column names in the first record of the table. Question 1: Am I right that BCP does not have a switch for t

相关标签:
1条回答
  • 2021-01-06 02:07

    bcp does not support exporting the column headers with the data, however there are some workarounds like exporting the headers in a separate file, then merging both the headers and data files as the following:

    exec master..xp_cmdshell 'BCP "select 'SETTINGS_ID','GROUP_NAME'" queryout d:\header.csv  -c  -T -t,'
    
    exec master..xp_cmdshell 'BCP "select SETTINGS_ID,GROUP_NAME from [DB]..[TABLE]" queryout "d:\columns.csv" -c -t, -T '
    
    exec master..xp_cmdshell 'copy /b "d:\header.csv"+"d:\columns.csv" "d:/result.csv"'
    

    You may also delete the unused files:

    exec master..xp_cmdshell 'del "d:\header.csv"'
    exec master..xp_cmdshell 'del "d:\columns.csv"'
    

    Or maybe you can combine all the data in a view (adding headers) and export it

    0 讨论(0)
提交回复
热议问题