Passing in current database name to DTS-based SQL task?

Hi all--I am writing a DTS package looking to back up all databases in sequence. I am calling CDOSYS in a stored procedure from support.microsoft.com to send email upon failure. I have two types of tasks I am writing into the DTS package:

1. backup database <database> to disk = '<drive>:\<path>.bak' with init

2. Upon failure, send the following message:

declare @srvname varchar(128),
@db_name varchar(128),
@msg nvarchar(4000)

select @srvname=srvname from master.dbo.sysservers
print @srvname
select @db_name=name from master.dbo.sysdatabases
print @db_name
set @msg='Hi--check your database backups. The ' + @db_name + ' database backup on ' + @srvname + ' has failed.

Thanks--SQL Server';

Upon success, go to the next database to back up, execute through these steps for the next database. Here's my problem: The @db_name variable captures the proper value for the current SQL Server, but I want to turn both @srvname and @db_name into global variables. I would like to pass in @db_name as the current database being backed up (and failing), and @srvname as the current SQL Server instance being backed up.

The above statement "select @db_name=name from master.dbo.sysdatabases" definitely does not capture the current database name value. Any suggestions as to how to pass this in, if not as a global variable?

Thanks in advance,

Jonathan

[1947 byte] By [jkv10] at [2007-12-24]
# 1

select db_name() will give you the name of the database that you are in

Denis the SQL Menace

http://sqlservercode.blogspot.com/

SQL_Menace at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

Hi Whitney and Denis--Thanks for the excellent replies, this solves the biggest part of my problem: this gets me the current database and instance name. The only obstacle remaining--and I'll probably pass this over to the SSIS forum for further assistance--is how to reset from one task to the next and pass in the current value of @db_name and @srvname once I've moved from the first database on to the next database.

If I try running Whitney's code in a SQL task for another database within the same package, it returns the previous value for @db_name and not the desired current database name (for example, the model database). So, I need to reset this value for each database based on the current backup SQL task in DTS and pass it over to the backup failure SQL task. Thanks all for the help thus far!

- Jonathan

jkv10 at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified