How to check if a database exists in SQL Server?

前端 未结 5 742
灰色年华
灰色年华 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条回答
  •  执念已碎
    2020-11-27 12:38

    IF EXISTS (SELECT name FROM master.sys.databases WHERE name = N'YourDatabaseName')
      Do your thing...
    

    By the way, this came directly from SQL Server Studio, so if you have access to this tool, I recommend you start playing with the various "Script xxxx AS" functions that are available. Will make your life easier! :)

提交回复
热议问题