Exporting to CSV file

%3CLINGO-SUB%20id%3D%22lingo-sub-1822681%22%20slang%3D%22en-US%22%3EExporting%20to%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1822681%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20are%20exporting%20some%20data%20from%20SQL%20to%20a%20CSV%20file%20which%20is%20then%20emailed.%20This%20whole%20process%20works%20great.%20However%2C%20the%20CSV%20file%20has%20------------------%20on%20Line%202%20of%20the%20CSV%20file.%20Is%20there%20a%20way%20to%20remove%20Line%202%20and%20all%20the%20--------------%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1822681%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBusiness%20Apps%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIntegration%20Services%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EReporting%20Services%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1822699%22%20slang%3D%22en-US%22%3ERe%3A%20Exporting%20to%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1822699%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20is%20what%20I%20am%20talking%20about%20in%20Line%202%2C%20see%20below.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22CSV1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F229503i45F60E90605880C0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22CSV1.png%22%20alt%3D%22CSV1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1826541%22%20slang%3D%22en-US%22%3ERe%3A%20Exporting%20to%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1826541%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F238560%22%20target%3D%22_blank%22%3E%40Tim%20Hunter%3C%2FA%3E%2C%20how%20do%20you%20export%20the%20data%20to%20a%20CSV%20file%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20result%20looks%20like%20an%20export%20from%20SSMS%20grid%2FQuery%20Window%2C%20right%3F%20Use%20better%20the%20Import%2FExport%20wizard%20for%20the%20export.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1827174%22%20slang%3D%22en-US%22%3ERe%3A%20Exporting%20to%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1827174%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F792867%22%20target%3D%22_blank%22%3E%40olafhelper%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20using%20the%20Database%20Mail%20feature%20with%20a%20Stored%20Procedure%20to%20automatically%20email%20the%20data%20in%20a%20CSV%20file.%20So%20it%20is%20not%20just%20exporting%20the%20CSV%20file.%20The%20CSV%20file%20is%20sent%20in%20an%20email%20directly%20from%20SQL.%20The%20stored%20procedure%20works%20perfectly%20and%20emails%20with%20no%20issue.%20The%20only%20problem%20I%20have%20is%20that%20Line%202.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sqlmail.png%22%20style%3D%22width%3A%20290px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F229803i75484357C2476583%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22sqlmail.png%22%20alt%3D%22sqlmail.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20our%20Stored%20Procedure%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESelect%20%40recordCount%20%3D%20ISNULL(count(*)%2C0)%20from%20invoiceheader%20%0Awhere%20ivh_billto%20%3D%20'PILKNO'%20%0Aand%20ivh_printdate%20%26gt%3B%3D%20dateadd(day%2C-7%2C%20getdate())%0A%0AIF%20(%40recordCount%20%26gt%3B%200)%0Abegin%0A%0ADECLARE%20%40FileName%20varchar(35)%0ASELECT%20%40FileName%20%3D%20'Wynne-'%20%2B%20convert(nvarchar(MAX)%2C%20dateadd(day%2C-7%2C%20getdate())%2C%2010)%20%2B%20'%20thru%20'%20%2B%20convert(nvarchar(MAX)%2C%20dateadd(day%2C-1%2C%20getdate())%2C%2010)%20%2B%20'.csv'%0AEXEC%20msdb..sp_send_dbmail%0A%20%40profile_name%20%3D%20'Pilot'%2C%0A%20%40recipients%20%3D%20'xxxx%40xxx.com%3Bxxx%40xxx.com'%2C%0A%20--%40from_address%20%3D%20'xxx%40xxx.com'%2C%0A%20--%40reply_to%20%3D%20'xxx%40xxx.com'%2C%0A%20%40subject%20%3D%20'Freight%20Invoices'%2C%0A%20--%40body%20%3D%20'Test%20CSV%20output'%2C%0A%20%40query%20%3D%20'EXEC%20%5Bdbo%5D.%5Bwynne_pilotinvoice%5D'%2C%0A%20%40execute_query_database%20%3D%20'TMWSUITE'%2C%0A%20%40attach_query_result_as_file%20%3D%201%2C%0A%20%40query_result_separator%20%3D'%20'%2C%0A%20%40query_result_header%20%3D1%2C%0A%20%40query_result_width%3D32767%2C%0A%20%40query_attachment_filename%20%3D%20%40FileName%0Aend%0Aelse%0A%20begin%0A%0A%20%20%20%20%20%20EXEC%20msdb.dbo.sp_send_dbmail%0A%20%20%20%20%20%20%40profile_name%20%3D%20'Pilot'%2C%0A%20%20%20%20%20%20%20%40recipients%20%3D%20'xxx%40xxx.com%3Bxxxx%40xxx.com'%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%40BODY%20%3D%20'No%20Invoices%20Found'%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%40subject%20%3D%20'Pilot%20SQL%20Job'%0A%0AEnd%0AEnd%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular Contributor

We are exporting some data from SQL to a CSV file which is then emailed. This whole process works great. However, the CSV file has ------------------ on Line 2 of the CSV file. Is there a way to remove Line 2 and all the --------------?

 

Thanks!

5 Replies

Here is what I am talking about in Line 2, see below.

CSV1.png

@Tim Hunter, how do you export the data to a CSV file?

 

The result looks like an export from SSMS grid/Query Window, right? Use better the Import/Export wizard for the export.

 

@olafhelper 

We are using the Database Mail feature with a Stored Procedure to automatically email the data in a CSV file. So it is not just exporting the CSV file. The CSV file is sent in an email directly from SQL. The stored procedure works perfectly and emails with no issue. The only problem I have is that Line 2.

 

sqlmail.png

 

Here is our Stored Procedure

Select @recordCount = ISNULL(count(*),0) from invoiceheader 
where ivh_billto = 'PILKNO' 
and ivh_printdate >= dateadd(day,-7, getdate())

IF (@recordCount > 0)
begin

DECLARE @FileName varchar(35)
SELECT @FileName = 'Wynne-' + convert(nvarchar(MAX), dateadd(day,-7, getdate()), 10) + ' thru ' + convert(nvarchar(MAX), dateadd(day,-1, getdate()), 10) + '.csv'
EXEC msdb..sp_send_dbmail
 @profile_name = 'Pilot',
 @recipients = 'xxxx@xxx.com;xxx@xxx.com',
 --@from_address = 'xxx@xxx.com',
 --@reply_to = 'xxx@xxx.com',
 @subject = 'Freight Invoices',
 --@body = 'Test CSV output',
 @query = 'EXEC [dbo].[wynne_pilotinvoice]',
 @execute_query_database = 'TMWSUITE',
 @attach_query_result_as_file = 1,
 @query_result_separator ='	',
 @query_result_header =1,
 @query_result_width=32767,
 @query_attachment_filename = @FileName
end
else
 begin

      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Pilot',
       @recipients = 'xxx@xxx.com;xxxx@xxx.com', 
            @BODY = 'No Invoices Found', 
            @subject = 'Pilot SQL Job'

End
End;

 

@olafhelper 

Here is the store procedure that builds the CSV file.

USE [TMWSUITE]
GO
/****** Object:  StoredProcedure [dbo].[wynne_pilotinvoice]    Script Date: 10/28/2020 8:31:03 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[wynne_pilotinvoice]
AS
BEGIN



select 
i.ivh_invoicenumber as [Carrier Invoice #]
,convert(nvarchar(MAX),i.ivh_billdate , 1) as [Date of Invoice]
,i.ivh_ref_number as [Pilot Company Order#]
,c.cmp_name as [BILL To: Business Entity]
,c.cmp_address1 as [BILL TO: Business Entity Address]
,ct.cty_name as [Terminal City]
,c1.cmp_address1 as [Terminal Address]
,f.fgt_supplier as [Lifted Supplier]
,( select top 1 ref_number 
	from referencenumber 
	where ref_type = 'BL#'
	and i.ord_hdrnumber = ord_hdrnumber) as [BOL#]
,i.ivh_totalvolume as [Lifted Quanity]
,convert(nvarchar(MAX),i.ivh_shipdate , 1) as [Lift Date]
,RIGHT(c2.cmp_name,3) as [Pilot Retail Location Number]
,c2.cmp_name as [Pilot Retail Location Name]
,c2.cmp_address1 as [Pilot Location Address]
,i.ivh_quantity as [Delivered Quanity]
,convert(nvarchar(MAX),i.ivh_deliverydate , 1) as [Delivery Date]
, i.ivh_splitbill_flag as [Split Load]
,f.fgt_description as [Lifted Product]
,i.ivh_rate as [Carrier Base Freight Rate]
,(	select SUM(ivd_charge) 
	from invoicedetail	
	where i.ivh_hdrnumber = ivh_hdrnumber 
	and cht_itemcode = 'FSCP') as [Fuel Surcharge]
,(	select SUM(ivd_charge) 
	from invoicedetail	
	where i.ivh_hdrnumber = ivh_hdrnumber 
	and cht_itemcode NOT IN ( 'FSCP','LHV')) as [Accesorial Charges Total]
,i.ivh_totalcharge as [Total Invoice]
from invoiceheader i
LEFT JOIN company c on i.ivh_billto = c.cmp_id
LEFT JOIN company c1 on i.ivh_shipper = c1.cmp_id 
LEFT JOIN company c2 on i.ivh_consignee = c2.cmp_id
LEFT JOIN city ct on i.ivh_origincity = ct.cty_code
LEFT JOIN city ct1 on i.ivh_destcity = ct1.cty_code
Left JOIN legheader l on i.ord_hdrnumber = l.ord_hdrnumber
LEFT JOIN freightdetail f on l.stp_number_rstart = f.stp_number
where i.ivh_billto = 'PILKNO' 
and i.ivh_printdate >= dateadd(day,-7, getdate()) 
order by i.ivh_billdate, i.ivh_invoicenumber

end

Hi @Tim Hunter --

One method for creating the csv with a result header with no lines is to generate the column names in the query section of sp_send_dbmail.  So for instance:

 @query = N'
			SET NOCOUNT ON
			SELECT ''RegionID'' AS RegionID, ''RegionDescription'' AS RegionDescription
			UNION ALL
			SELECT 
				CAST(RegionID AS varchar(10)),RegionDescription
			FROM
				TestDatabase01..mailtest'

Also, set the @query_result_header = 0.  The contents of the CSV should appear similar to this:

bake13_0-1603939305863.png

One recommendation I might make is that since you are using an existing stored procedure, considering sending the results of the stored procedure to a temp table, the writing a SELECT statement similar to the one shown above to produce and send the CSV.  Take care.