For weeks I've been searching the answer for this problem I have. I need to send by email query results from the day before in an excel sheet.
Requirements are:
Problems:
My half-working code now:
use xxx
DECLARE @AWBPREFIX VARCHAR(255)
DECLARE @Query VARCHAR(MAX)
SET @AWBPREFIX = '[sep=,' + CHAR(13) + CHAR(10) + 'AWB.PREFIX]'
SET @Query = '
SET NOCOUNT ON
SELECT AWB.PREFIX AS ' + @AWBPREFIX + ',
AWB.SERIAL,
AWB.ORIGIN,
AWB.DEST,
AWB_MOVE.CARRIER,
AWB.PIECES,
AWB.WEIGHT,
AWB_MOVE.PIECES,
AWB_MOVE.WEIGHT,
AWB.NATURE_GOODS,
AWB_MOVE.CARRIER,
AWB_MOVE.FLIGHT_NUMBER,
AWB_MOVE.FLIGHT_DATE,
AWB_MOVE.ORIGIN,
AWB_MOVE.DEST,
AWB_MOVE.ULD_NUMBER,
AWB_MOVE.SHC_LIST
FROM
xxx..AWB
INNER JOIN
AWB_MOVE ON AWB.SEQ = AWB_MOVE.AWB_SEQ
GROUP BY
AWB.PREFIX,
AWB.SERIAL,
AWB.ORIGIN,
AWB.DEST,
AWB_MOVE.CARRIER,
AWB.PIECES,
AWB.WEIGHT,
AWB_MOVE.PIECES,
AWB_MOVE.WEIGHT,
AWB.NATURE_GOODS,
AWB_MOVE.CARRIER,
AWB_MOVE.FLIGHT_NUMBER,
AWB_MOVE.FLIGHT_DATE,
AWB_MOVE.ORIGIN,
AWB_MOVE.DEST,
AWB_MOVE.ULD_NUMBER,
AWB_MOVE.SHC_LIST,
AWB_MOVE.action_status
HAVING AWB_MOVE.FLIGHT_DATE = dateadd(dd,-1,cast(getdate() as date)) AND AWB_MOVE.DEST ="BRU" AND AWB_MOVE.action_status <>"NOT"'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Batch',
@recipients = 'xxx',
@execute_query_database = 'xxx',
@subject ='Import extracts',
@query =@Query,
@query_result_header = 1,
@query_result_separator=',',
@query_result_no_padding=1,
@query_result_width=32767,
@attach_query_result_as_file = 1,
@query_attachment_filename = 'import_batch.csv'
I cannot use SSIS due to office restrictions.
Can someone help me please?
Thanks Michael
sp_send_dbmail will not make a .xlsx file for you. However, you can use the method described here to make the .csv from sp_send_dbmail readable in excel.
If you have SSRS in your environment, you might be able to create a report subscription which publishes to excel.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.