How to check if a database exists in SQL Server?

前端 未结 5 756
灰色年华
灰色年华 2020-11-27 11:55

What is the ideal way to check if a database exists on a SQL Server using TSQL? It seems multiple approaches to implement this.

5条回答
  •  旧时难觅i
    2020-11-27 12:48

    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

提交回复
热议问题