By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,583 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,583 IT Pros & Developers. It's quick & easy.

How to email completion messages from RESTORE commands?

P: n/a
I need to build an automated email that gives the completion messages
when a database is restored (i.e. "Executed as user: sa. Executing
RESTORE DATABASE DB1 FROM
DISK='h:\backups\DB1\DB1_db_200411082056.BAK', RECOVERY [SQLSTATE
01000] (Message 0) Processed 3816 pages for database 'DB1', file
'DB1_Data' on file 1. [SQLSTATE 01000] (Message 4035) Processed 1
pages for database 'DB1', file 'DB1_Log' on file 1. [SQLSTATE 01000]
(Message 4035)")

Currently, the Job History box contains it, but I'd rather get it via
email. The base restore statement works, but it gives me a "Cannot
perform a backup or restore operation within a transaction." when I
try to run it as below.

works:
[build @RestoreCmd]
exec (@RestoreCmd)

doesn't:
[build @RestoreCmd]
create table #Error_Finder (listing nvarchar (4000))
declare @Errors smallint
insert #Error_Finder exec (@RestoreCmd)
EXEC xp_sendmail @recipients = 'dba',
@query = 'SELECT * from #Error_Finder',
@subject = 'SQL Server Restores'
drop table #Error_Finder
Any suggestions? My next thought is to start selecting against system
tables in msdb. It looks because the Insert can fail, it's a
transaction.
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Michale
In advanced tab of the job step window click 'edit' there you can define
where to go in success or on failure. Create two steps like 'send OK', and
'send Failed' that will be notified you about restore.


"Michael Bourgon" <bo*****@gmail.com> wrote in message
news:55**************************@posting.google.c om...
I need to build an automated email that gives the completion messages
when a database is restored (i.e. "Executed as user: sa. Executing
RESTORE DATABASE DB1 FROM
DISK='h:\backups\DB1\DB1_db_200411082056.BAK', RECOVERY [SQLSTATE
01000] (Message 0) Processed 3816 pages for database 'DB1', file
'DB1_Data' on file 1. [SQLSTATE 01000] (Message 4035) Processed 1
pages for database 'DB1', file 'DB1_Log' on file 1. [SQLSTATE 01000]
(Message 4035)")

Currently, the Job History box contains it, but I'd rather get it via
email. The base restore statement works, but it gives me a "Cannot
perform a backup or restore operation within a transaction." when I
try to run it as below.

works:
[build @RestoreCmd]
exec (@RestoreCmd)

doesn't:
[build @RestoreCmd]
create table #Error_Finder (listing nvarchar (4000))
declare @Errors smallint
insert #Error_Finder exec (@RestoreCmd)
EXEC xp_sendmail @recipients = 'dba',
@query = 'SELECT * from #Error_Finder',
@subject = 'SQL Server Restores'
drop table #Error_Finder
Any suggestions? My next thought is to start selecting against system
tables in msdb. It looks because the Insert can fail, it's a
transaction.

Jul 20 '05 #2

P: n/a
[posted and mailed, please reply in news]

Michael Bourgon (bo*****@gmail.com) writes:
Currently, the Job History box contains it, but I'd rather get it via
email. The base restore statement works, but it gives me a "Cannot
perform a backup or restore operation within a transaction." when I
try to run it as below.

works:
[build @RestoreCmd]
exec (@RestoreCmd)

doesn't:
[build @RestoreCmd]
create table #Error_Finder (listing nvarchar (4000))
declare @Errors smallint
insert #Error_Finder exec (@RestoreCmd)
EXEC xp_sendmail @recipients = 'dba',
@query = 'SELECT * from #Error_Finder',
@subject = 'SQL Server Restores'
drop table #Error_Finder


Even if there is no user-defined transaction, an INSERT, UPDATE or
DELETE statement is its own transaction in SQL Server. This means that
INSERT EXEC() defines a transaction.

Furthermore, even if RESTORE had not cared about the transaction, it
would not have worked anyway, because INSERT EXEC() can only catch
result set, and what RESTORE produces is an informational message,
which is passed to the client. There is no way to catch this message
in the server.

Uri's suggestion of using the GUI to set up a e-mail alert, sounds like
a much easier way to go.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.