if we want to check database exists or not we can know four way.
-- Solution number (1) DECLARE @Database_Name varchar(100) SET @Database_Name='forum' IF EXISTS(SELECT * FROM sys.sysdatabases where name=@Database_Name) PRINT 'The database already exists' else PRINT 'The database does not exist'
-- Solution number (2) DECLARE @Database_Name varchar(100) SET @Database_Name='forum' IF EXISTS(SELECT * FROM master..sysdatabases WHERE name=@Database_Name) PRINT 'The database already exists' else print 'The database does not exist'
-- Solution number (3) DECLARE @Database_Name varchar(100) SET @Database_Name='forum' DECLARE @sql varchar(1000) SET @sql='if ''?''='''+@Database_Name+''' print ''the database already alexists''' EXEC sp_msforeachdb @sql
-- Solution number (4) DECLARE @Database_Name varchar(100) SET @Database_Name='forum' DECLARE @sql varchar(1000) SET @sql='if exists(select * from ?.information_schema.schemata where catalog_name='''+@Database_Name+''') print ''the database already exists''' EXEC sp_msforeachdb @sql
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
if we want to check database exists or not we can know four way.