Actually, it’s best to use:
IF DB_ID('dms') IS NOT NULL
--code mine :)
print 'db exists'
See https://docs.microsoft.com/en-us/sql/t-sql/functions/db-id-transact-sql and note that this does not make sense with the Azure SQL Database.
More Related Contents:
- Create a view with ORDER BY clause
- What does ‘COLLATE SQL_Latin1_General_CP1_CI_AS’ do?
- TSQL: Create a view that accesses multiple databases
- How do I create a unique constraint that also allows nulls?
- How do I get list of all tables in a database using TSQL?
- Get first day of week in SQL Server
- What is the significance of 1/1/1753 in SQL Server?
- Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?
- Escape a string in SQL Server so that it is safe to use in LIKE expression
- Insert Update trigger how to determine if insert or update
- SQL Server: Difference between PARTITION BY and GROUP BY
- How do you truncate all tables in a database using TSQL?
- Understanding SQL Server LOCKS on SELECT queries
- SQL Server: How to select all days in a date range even if no data exists for some days
- SQL – The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- TSQL: How to convert local time to UTC? (SQL Server 2008)
- T-SQL Dynamic SQL and Temp Tables
- Need to list all triggers in SQL Server database with table name and table’s schema
- SQL Server Output Clause into a scalar variable
- How to create a yes/no boolean field in SQL server?
- Best way to store time (hh:mm) in a database
- How do I select last 5 rows in a table without sorting?
- SELECT INTO a table variable in T-SQL
- How can I insert binary file data into a binary SQL field using a simple insert statement?
- Cannot find either column “dbo” or the user-defined function or aggregate “dbo.Splitfn”, or the name is ambiguous
- Date range overlapping check constraint
- Include missing months in Group By query
- Pivot multiple columns based on one column in SQL Server
- @@ERROR and/or TRY – CATCH
- Tool for Scripting Table Data