Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sp_send_dbmail

Author  Topic 

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-05 : 15:45:12
Hello,

When I use: @query_attachment_filename = test.xls

Using dbmail stored procedure,

The results of the query are populated into an excel sheet. The problem is that the formatting is horrible! Is there any solution as to how to properly format the data when it is populating into excel?

Thank you in advance

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-01-05 : 16:13:29
Check the forum faq..
[url]
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210[/url]

You will be most interested to look at BCP (bulk copy program).

You can use other techniques like DTS as well
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-05 : 16:44:45
You can't just pass "xls" as the extension and have it put it into proper Excel format. No matter what extension you use, it'll have the same format. The format is just text.

To get better formatting, bcp the data out to a csv file and then attach the csv file to the email. Excel understands csv format very well.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-05 : 17:09:41
Great, thanks guys, I am working on this and will let you know how it goes! Appreciate it.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-05 : 17:14:34
You're welcome. Let us know if you need help with bcp. Here's a recent thread that shows some code that I just setup using bcp: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=117097

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-06 : 14:56:37
So I have a stored procedure that I need to bcp into excel (i guess using a .csv file).

Can I write it as follows:?:

'bcp "exec sp_mySP @dataareaid = ''xxx''" queryout "C:\test.csv" -c -t -S"'
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 15:02:33
You'll need to also specify -T or -U/-P so that it knows how to connect. You probably will need -r, I always use -r\r\n unless someone specifically needs one or the other. You need to specify -t, and the server name for -S.

Yes you can use a stored procedure in the queryout.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-01-06 : 15:21:23
sqlsoaker

Here is one of my export routines using BCP.. tested and verified on sql2005 server instance


DECLARE @cmd VARCHAR(2048)
SET @cmd = 'bcp '
+ ' "SELECT Manufacturer,ManufacturerPartNumber,CAST(List as decimal(10,2)),'
+ ' CAST(Cost as decimal(10,2)),CAST(Price as decimal(10,2)),CONVERT(varchar(12),CustomDate01,101) '
+ ' FROM MyDataBaseName.dbo.Products Order by Manufacturer,ManufacturerPartNumber" queryout'
+ ' D:\Excel_Exports\myfile.csv -c -t\, -r \n -T'

EXEC master..xp_cmdshell @cmd
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-06 : 15:40:43
Thank you,

Can someone please help with the syntax, I'm getting rather confused over what arguments are needed, I have the "man" page for the bcp utility but still confused:

'bcp "exec sp_mySP @dataareaid = ''xxx''" queryout 'C:\Excel\myfile.csv -c -t\, -r \n -T'


Tara, you mentioned that I need to use -T but how? Like this? Same with -S what is the syntax to give it the server name???

You all rock.

SQLSoaker
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 16:01:30
-T means to connect with Windows authentication. If you instead want SQL authentication, then you'd use -U/-P.

Are you going to run it from xp_cmdshell, batch file, or from a cmd window?

I posted this earlier in the thread, but I'll post it again in case it was missed:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=117097

It shows working bcp code for a recent task that I had.

You'll for sure need to get rid of that single quote before C:\.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-06 : 16:02:44
Used this:

DECLARE @cmd VARCHAR(2048)
SET @cmd = 'bcp '
+ ' "exec sp_mySP @dataareaid = ''xxx''" queryout'
+ ' C:\myfile.csv -c -t\, -r \n -T'

EXEC master..xp_cmdshell @cmd


Getting an error saying cmdshell is not turned on, so following these steps I tried to turn it on...

http://www.tech-recipes.com/rx/2027/sql_server_2005_how_to_enable_xp_cmdshell/

But got an additional error:

An error has occured while establishing a connection to the server. When connecting to the SQL Server 2005, this failure may be cause by the fact thatt under the default settings SQL server does not allow remote connections.



--UGH
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-06 : 16:06:28
Is there another way to turn on cmdshell???
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-06 : 16:31:24
EXEC sp_configure 'xp_cmdshell', 1
GO
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 17:08:00
Here you go:
http://weblogs.sqlteam.com/tarad/archive/2006/09/14/12103.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 17:08:40
You are getting that second error as you are missing the -S switch. You need to tell it what to connect to.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-08 : 15:41:38
Here is what I am trying to accomplish:

Have a stored procedure for a report that needs to be automated to go to certain users:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Email_theSP] AS
BEGIN
SET NOCOUNT ON
-- do some other actions
DECLARE @body1 VARCHAR(1024)
SET @body1 = 'Hi'

This message was sent on:'+ CONVERT(VARCHAR, GETDATE())
EXEC msdb.dbo.sp_send_dbmail
@recipients='me@me.com',
@subject = 'SQL Test Mail - Testing Automation ,
@body= @body1,
@execute_query_database = 'myDB',
@query = 'sp_mySP @dataareaid = ''xxx''',
@attach_query_result_as_file = 1,
@query_result_width = 1500,
@query_attachment_filename = 'testing.xls'

END

The stored procedure sp_mySP:

select .... from ... where .... join ....


Can I use bcp with the @query parameter in dbmail???

Something like this:
@query = 'bcp sp_mySP @dataareaid = ''xxx'' queryout' C:\myfile.csv -c -t\, -r \n -T -S[myServerName]'


I am struggling with the syntax so it is erroring out at the moment. Right now I am simply trying to call bcp in a script


'bcp sp_mySP @dataareaid = ''xxx'' queryout' C:\myfile.csv -c -t\, -r \n -T -S[myServerName]'

But keep getting these errors:

Password:
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = HYT00, NativeError = 0
Error = [Microsoft][SQL Native Client]Login timeout expired
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connecti
NULL



I am doing all of this so the data's formatting is clean in the report that needs to go to the users...

Plz help with syntax!

Regards.




Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-08 : 15:52:31
You can not use bcp in the @query parameter. bcp will need to be run from xp_cmdshell. You can then email the bcp file, but don't use the @query parameter as the data is already available in the file.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-08 : 16:17:26
quote:
Originally posted by tkizer

You can not use bcp in the @query parameter. bcp will need to be run from xp_cmdshell. You can then email the bcp file, but don't use the @query parameter as the data is already available in the file.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




So you are saying instead I have to use this in the @query string:

EXEC master..xp_cmdshell @cmd


????
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-08 : 16:19:38
No, run that as a separate command in your stored procedure and then just add the resultant file to the email as an attachment.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2009-01-08 : 16:26:44
quote:
Originally posted by tkizer

No, run that as a separate command in your stored procedure and then just add the resultant file to the email as an attachment.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Hummmm....

How should I plan on altering the SP??? Do I bcp the entire SP...?

'bcp (very long query) -T -S ....'
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-08 : 16:42:51
No, just add your bcp call to the stored procedure. Have you gotten the bcp command to work yet through xp_cmdshell? You need to get that part to work first before even bothering with the email component.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
    Next Page

- Advertisement -