We have a requirement to run a SQL Server 2000 stored procedure and then write the results to a CSV file which needs to be emailed. Ideally all this to be automated to be done once daily. Anybody have any ideas? Thanx Alexis
Of course. I do this w/several jobs -- use DTS, a transform data task from server to csv file. The transform source is your procedure, the destination is the csv file, the transformation is just a 1 to 1 of your proceduer resultset -- i.e., you procedure returns Date,User,Volume -- that's your transformation.
then you've got a sql agent job, 1st step invokes the DTS pkg, like this: exec master..xp_cmdshell 'DTSRUN /S "servername" /E 'N "packagename" '
the 2nd step in your job is the email. i do mine like this:
EXEC master..xp_sendmail @recipients ='whomeverYourSendingItto@wherever.com', @blind_copy_recipients = 'maybeYourself@wherever.com', @attachments = '\\servername\h$\filename.csv', @subject='subject line for the email', @message='any message you'd like to send along with the attachment'
The first step running fine apart from a small typo with the N parameter in the syntax - I think it hould read : exec master..xp_cmdshell 'DTSRUN /S "servername" /E /N "packagename" '
The second one have not had as much joy. The SQL Server agent job appears to run OK but no mail is sent - I kept it simple by not even having an attachment just to make sre that an email can be sent. I seem to remember that there is something that needs to be set up before I can run x_sendmail. Any ideas?
oh yes, i'm sorry, i should have given more detail. i use smtp and my own version of xp_sendmail. see here:
/****** Object: Stored Procedure dbo.xp_sendmail Script Date: 10/18/2006 9:25:17 AM ******/ SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO
/****** Object: Stored Procedure dbo.xp_sendmail Script Date: 10/18/2006 9:25:17 AM ******/ -- create replacement xp_sendmail procedure CREATE proc dbo.xp_sendmail @recipients varchar(8000) = NULL, @message varchar(8000) = NULL, @query varchar(8000) = NULL, -- parameter will not be used @attachments varchar(8000) = NULL, @copy_recipients varchar(8000) = NULL, @blind_copy_recipients varchar(8000) = NULL, @subject varchar(8000) = 'SQL Server Message', @type varchar(8000) = NULL, @attach_results varchar(8000) = NULL, -- parameter will not be used @no_output varchar(8000) = 'FALSE',-- parameter will not be used @no_header varchar(8000) = 'FALSE',-- parameter will not be used @width int = 80, -- parameter will not be used @separator varchar(8000) = NULL, -- parameter will not be used @echo_error varchar(8000) = NULL, -- parameter will not be used @set_user varchar(8000) = NULL, -- parameter will not be used @dbuse varchar(8000) = NULL -- parameter will not be used as set nocount on
declare @rc int exec @rc = master.dbo.xp_smtp_sendmail @FROM = N'YOURSERVERNAME', -- change value to suit your own needs @FROM_NAME = N'YOURSERVERNAME', -- change value to suit your own needs @TO = @recipients, @replyto = 'YOURNAME@WHEREVER.COM', -- change value to suit your needs @CC = @copy_recipients, @BCC = @blind_copy_recipients, @priority = N'normal', -- change value to suit your own needs @subject = @subject, @message = @message, -- no equivalent parameter value available @type = N'text/plain', -- change value to suit your own needs @attachments = @attachments, -- @attach_results = @attach_results, @server = N'YOURMAILSERVER.COM', -- change value to suit, like mail.domain.com @timeout = 10000 -- change value to suit your own needs
if (@@error <> 0 or @rc <> 0) raiserror(N'Sending message using xp_sendmail failed', 16, 1)
Hi there dbaSQL. I got sidetracked on another project and will be returning to this tomorrow, so please accept my apologies for the silence - you know how it is.