SQL Server 2008 R2 - remove line below heading (which has ----,----) - dont have SSIS I have found this line of code online

sqlcmd -S localhost -d master -E -W -w 999 -s "," -Q "SELECT D.* FROM sys.databases D" -o "C:\sqlcmd.csv"


This is used in a SQL job, to export to a CSV file.

There is no SSIS installed, I cannot install SSIS on the machine.

This is what is exported

name,database_id,source_database_id,owner_sid,create_date,compatibility_level,collation_name,user_access,user_access_desc,is_read_only,is_auto_close_on,is_auto_shrink_on,state,state_desc,is_in_standby,is_cleanly_shutdown,is_supplemental_logging_enabled,snapshot_isolation_state,snapshot_isolation_state_desc,is_read_committed_snapshot_on,recovery_model,recovery_model_desc,page_verify_option,page_verify_option_desc,is_auto_create_stats_on,is_auto_update_stats_on,is_auto_update_stats_async_on,is_ansi_null_default_on,is_ansi_nulls_on,is_ansi_padding_on,is_ansi_warnings_on,is_arithabort_on,is_concat_null_yields_null_on,is_numeric_roundabort_on,is_quoted_identifier_on,is_recursive_triggers_on,is_cursor_close_on_commit_on,is_local_cursor_default,is_fulltext_enabled,is_trustworthy_on,is_db_chaining_on,is_parameterization_forced,is_master_key_encrypted_by_server,is_published,is_subscribed,is_merge_published,is_distributor,is_sync_with_backup,service_broker_guid,is_broker_enabled,log_reuse_wait,
log_reuse_wait_desc,is_date_correlation_on,is_cdc_enabled,is_encrypted,is_honor_broker_priority_on
----,-----------,------------------,---------,-----------,-------------------,--------------,-----------,----------------,------------,----------------,-----------------,-----,----------,-------------,-------------------,-------------------------------,------------------------,-----------------------------,-----------------------------,--------------,-------------------,------------------,-----------------------,-----------------------,-----------------------,-----------------------------,-----------------------,----------------,------------------,-------------------,----------------,-----------------------------,------------------------,-----------------------,------------------------,----------------------------,-----------------------,-------------------,-----------------,-----------------,--------------------------,---------------------------------,------------,-------------,------------------,--------------,-------------------,-------------------,-----------------,--------------,
-------------------,----------------------,--------------,------------,---------------------------
master,1,NULL,0x01,2010-04-08 09:13:36.390,100,SQL_Latin1_General_CP1_CI_AS,0,MULTI_USER,0,0,0,0,ONLINE,0,0,0,1,ON,0,3,SIMPLE,2,CHECKSUM,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0xxxx0-0000-0000-0000-000000000000,0,0,NOTHING,0,0,0,0
tempdb,2,NULL,0x01,2014-09-15 08:39:08.587,100,SQL_Latin1_General_CP1_CI_AS,0,MULTI_USER,0,0,0,0,ONLINE,0,0,0,0,OFF,0,3,SIMPLE,2,CHECKSUM,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,Exxxxx7-BE30-4829-8AB1-DAA8730978AE,1,0,NOTHING,0,0,0,0


Is there anyway to amend the command in order to exclude the line below the heading please, I need the heading, but they can't accept the line below the heading (the line starting with ----,--)
Unfortunately, there is no way directly from BCP to get the header as well as the rows out in a pretty format, even with the out parameter

HOWEVER, there is a workaround. You can do this:

SELECT 'HeaderCol1Name', 'HeadCol2Name'
UNION ALL
SELECT CAST(C.Col1 AS VARCHAR(255)), CAST(C.Col2 AS VARCHAR(255)) ...
FROM (SELECT &lt;Your original query&gt;) AS C


You can now:

BCP &lt;above query in quotes&gt; out Foo.txt 


Yes, it is ugly. And yes, it works. You can even control things like date formatting this way.