How to Remove the Column Headers when Emailing Query Results in SQL Server (T-SQL)

When you use the sp_send_dbmail stored procedure to email the results of a query, the column headers are included by default.

You can include or exclude the column headers with the @query_result_header argument. To remove the column headers, use @query_result_header = 0.

Example

Here’s an example to demonstrate.

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'DB Admin Profile',  
    @recipients = '[email protected]',  
    @body = 'Top 5 cities:',
    @query = 'SELECT TOP(5) * FROM city;',
    @execute_query_database = 'World',
    @query_result_header = 0,
    @query_result_no_padding = 1,
    @subject = 'Query results as discussed';

Result:

Top 5 cities:
1 Kabul AFG Kabol 1780000
2 Qandahar AFG Qandahar 237500
3 Herat AFG Herat 186800
4 Mazar-e-Sharif AFG Balkh 127800
5 Amsterdam NLD Noord-Holland 731200

(5 rows affected)

In this example I also used @query_result_no_padding = 1 to remove any padding that might be applied to the columns.

If you prefer each column to be separated by a comma, use @query_result_separator = ','.

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'DB Admin Profile',  
    @recipients = '[email protected]',  
    @body = 'Top 5 cities:',
    @query = 'SELECT TOP(5) * FROM city;',
    @execute_query_database = 'World',
    @query_result_header = 1,
    @query_result_no_padding = 1,
    @query_result_separator = ',',
    @subject = 'Query results as discussed';

Result:

Top 5 cities:
ID,Name,CountryCode,District,Population
--,----,-----------,--------,----------
1,Kabul,AFG,Kabol,1780000
2,Qandahar,AFG,Qandahar,237500
3,Herat,AFG,Herat,186800
4,Mazar-e-Sharif,AFG,Balkh,127800
5,Amsterdam,NLD,Noord-Holland,731200

(5 rows affected)

Include Column Headers

To explicitly include column headers, use @query_result_header = 1.

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'DB Admin Profile',  
    @recipients = '[email protected]',  
    @body = 'Top 5 cities:',
    @query = 'SELECT TOP(5) * FROM city;',
    @execute_query_database = 'World',
    @query_result_header = 1,
    @query_result_no_padding = 1,
    @subject = 'Query results as discussed';

Result:

Top 5 cities:
ID Name CountryCode District Population
-- ---- ----------- -------- ----------
1 Kabul AFG Kabol 1780000
2 Qandahar AFG Qandahar 237500
3 Herat AFG Herat 186800
4 Mazar-e-Sharif AFG Balkh 127800
5 Amsterdam NLD Noord-Holland 731200

(5 rows affected)