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
 Automatic emails

Author  Topic 

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2008-12-16 : 12:43:03
Hello,

I have a stored procedure that emails a query. I need to know how to schedule this procedure to run at specific times for t-sql 2005.

I have found info on this for sql 2000 but not 05'.

Thanks in advance.

Regards.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 12:47:32
You can schedule a stored procedure to run via a SQL job. Check out the SQL Server Agent jobs in Management Studio. Also be sure to check out the documentation, Books Online.

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

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-16 : 12:47:39
Use jobs in SQL Server Agent.You need to have permission to create and run it.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 12:52:13


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 - 2008-12-16 : 14:36:26
Thankyou tkizer once again. One more question: Now that I have created a job and scheduled it, how do you tell the job to execute the stored procedure? Thank you! (Again, I am new so please excuse)
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2008-12-16 : 14:43:11
Steps! Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 14:44:42
I'm not sure I understand your question. You put the stored procedure call into a job step and then you add a job schedule. After that, the SQL Server Agent handles the rest.

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 - 2008-12-16 : 15:35:49
quote:
Originally posted by tkizer

I'm not sure I understand your question. You put the stored procedure call into a job step and then you add a job schedule. After that, the SQL Server Agent handles the rest.

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

Subscribe to my blog




That's exactly what I am looking for!

Also, can you attach multiple query's to the email??? If so, how to???
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 15:55:04
What are you using for an email solution?

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 - 2008-12-16 : 16:37:19
quote:
Originally posted by tkizer

What are you using for an email solution?

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

Subscribe to my blog




Simply emailing from a mail server that runs Outlook... The problem is that I don't want to alter the SP that we currently have. However, I do need to run it with various parameters in each email.

I have tested running multiple parameter like : 1001..1010

No luck.

If dbmail would allow multiple @query param's then that would solve it... Something like

@query
@query1
@query2
etc. etc.

Either that or something like:

@query = 'select * from table1, select * from table2, select * from table3...

If all the query results can go to a file or simply in the body of the email, that would be great!...! But how???

Thanks again tkizer, you've been a real help!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 16:41:49
Then just call the stored procedure multiple times, once for each parameter.

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 - 2008-12-16 : 17:03:29
quote:
Originally posted by tkizer

Then just call the stored procedure multiple times, once for each parameter.

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

Subscribe to my blog




All in one step! Yes! Brilliant!

So you're saying call multiple times in one step?

exec sp_mySP @parm = '1'
exec sp_mySP @parm = '2'
exec sp_mySP @parm = '3'

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 17:11:36
Yes. You could choose to use 3 job steps, which could make it easier when looking back at job history for failures. But I'd probably just use one job step, cuz I'm lazy.

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 - 2008-12-16 : 17:23:25
quote:
Originally posted by tkizer

Yes. You could choose to use 3 job steps, which could make it easier when looking back at job history for failures. But I'd probably just use one job step, cuz I'm lazy.

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

Subscribe to my blog


Okay, I'm kind of confused about what to do here...

I have a SP (lets call it sp_myMail)that generates an email thru DBmail that calls another SP (lets call it sp_mySP). Currently I set up a job that calls the stored procedure:

exec sp_mySP

This sp calls another (sp_myMail) where I run the parameters. Something like:

@query= 'exec sp_myMail @parm1 ='' @parm2 ='''

tkizer, what you have suggested is calling the sp multiple times. But I am not calling the sp_myMail from the job, I am calling sp_mySP so I can't use multiple sp calls in one step!

Also, I can't run multiple queries in DBMail like this:

@query = 'select * from table1, select * from table2 etc.'

Am I missing something here? I can set up a job to run sp_mySP but that doesn't help because it does not utilize DBMail (its just a query in the stored procedure!)

Ugh, I hope I am not confusing!



Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2008-12-16 : 17:32:51
want to clarify a little:

This is the sp that calls another.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[testEmailattachSP_28] AS
BEGIN
SET NOCOUNT ON
-- do some other actions
DECLARE @body1 VARCHAR(1024)
SET @body1 = 'This is a test to execute a stored procedure thru a job. During this test you should receieve an email every morning at 7 AM. The attached file is the aging bucket report for account number 100001 for xxx. This message was sent on:'+
CONVERT(VARCHAR, GETDATE())

EXEC msdb.dbo.sp_send_dbmail
@recipients='me.me@me.com;,
@body= @body1,
@execute_query_database = 'xxx_xxx_Production',
@query = 'sp_AgingBuckets @dataareaid = ''xxx'', @accountnum = ''100001''',
--@query = 'select top(5)accountnum, name from custtable where dataareaid = ''xxx''',
@attach_query_result_as_file = 1,
--@query_result_width = 250,
@query_attachment_filename = 'testing.xls'

END


Now I scheduled a job to run this command (in one step):

exec testEmailattachSP_28


But I need to run sp_AgingBuckets using multiple parameters and generate one email from all of em'.

Ahhh!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 17:38:54
I'm confused too. Can't you just call sp_mySP multiple times? Doesn't it have input parameters that you then pass to sp_myMail?

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 - 2008-12-16 : 17:56:28
quote:
Originally posted by tkizer

I'm confused too. Can't you just call sp_mySP multiple times? Doesn't it have input parameters that you then pass to sp_myMail?

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

Subscribe to my blog




Not all within one email!!!

Or unless you can do this using DBMail:

@query = 'exec sp_AgingBuckets @parm = '1', exec sp_AgingBuckets @parm = '2', exec sp_AgingBuckets @parm = '3''

But you can't!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-16 : 18:00:11
That's not what I meant. Call sp_mySP 3 different times, so that 3 emails are sent out. Otherwise, you are going to need to modify the stored procedure to attach multiple files.

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 - 2008-12-17 : 09:24:18
quote:
Originally posted by tkizer

That's not what I meant. Call sp_mySP 3 different times, so that 3 emails are sent out. Otherwise, you are going to need to modify the stored procedure to attach multiple files.

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

Subscribe to my blog




Understood, but calling sp_mySP three times will send out three emails. I want to call this once and generate one email with three different queries attached (those queries are another call to an sp).

Going in circles on this but let me try to explain what I want.

I want to be able to call the sp that sends the email once. But within that sp I want to be able to attach MULTIPLE query results (this is another call to another sp) to either a file or the body of the email. Currently I cannot do this because DBMail only accepts one query.

So my job is scheduled to call the sp once, which is what I want, but I want to attach multiple results to a file...

I am not sure that this could be done with just one email!!!

PS: tkizer, i really appreciate the help on this!
Go to Top of Page

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2008-12-17 : 09:41:20
Figured something out. Created yet another stored procedure that executes the sp multiple times. Then in the sp_mail procedure I execute this one. (multiple exec calls to the sp). This gives me multiple results in the file attachment in the email!
Go to Top of Page
   

- Advertisement -