Når du bruger sp_send_dbmail
gemt procedure til at sende resultaterne af en forespørgsel via e-mail, er kolonneoverskrifterne inkluderet som standard.
Du kan inkludere eller ekskludere kolonneoverskrifterne med @query_result_header
argument. For at fjerne kolonneoverskrifterne, brug @query_result_header = 0
.
Eksempel
Her er et eksempel til at demonstrere.
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';
Resultat:
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)
I dette eksempel brugte jeg også @query_result_no_padding = 1
for at fjerne eventuel polstring, der kan påføres søjlerne.
Hvis du foretrækker, at hver kolonne er adskilt med et komma, skal du bruge @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';
Resultat:
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)
Medtag kolonneoverskrifter
For eksplicit at inkludere kolonneoverskrifter, brug @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';
Resultat:
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)