What's the ideal way to see if a database is available on the SQL Server using TSQL? It appears multiple methods to implement this.
From the Microsoft's script:
DECLARE @dbname nvarchar(128) SET @dbname = N'Senna' IF (EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE ('[' + name + ']' = @dbname OR name = @dbname))) -- code mine :) PRINT 'db exists'
Really it's more optimal to make use of:
if db_id('dms') is not null --code mine :) print 'db exists'
USE [master] GO IF EXISTS (SELECT name FROM sys.databases WHERE name = N'YourDatabaseName') Do your thing...
Incidentally, this came from SQL Server Studio, therefore if you can get it, I suggest you start having fun with the different "Script xxxx AS" functions that are offered. Can make your existence simpler! :)