Possible Duplicate:
How to check if a database exists in SQL Server?
I'm using Transact-SQL to attach a SQL Server 2008 R2 database with the following code, but I'd like to do this only if the database is not already attached, because trying to attach an already attached database returns an error.
Is there a way using Transact-SQL to test if the database is already attached?
USE [MASTER]
GO
#I'd like to test here if database is already attached
CREATE DATABASE ASPNETDB
ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Data\ASPNETDB.MDF')
FOR ATTACH ;
GO