[英]Checking if database exists or not in SQL Azure
谁能告诉我如何在sql azure中编码数据库是否存在?
您是否尝试查询sys.databases表? 那应该给你你想要的东西。 更多信息在这里 。
注意:您需要对Master数据库运行此查询。 否则,您只会看到当前数据库(和主数据库)的名称。
Select count(*) from sysobjects where name = 'testdb'
如果未找到Select count(*) from sysobjects where name = 'testdb'
返回0)。 输入数据库的名称,我们将为您编辑脚本..您所需要做的就是复制并粘贴好..? 这是您可以尝试的一些其他方法
方法1:使用sys.sysdatabases视图
IF EXISTS(SELECT * FROM sys.sysdatabases where name=@testdb)
PRINT 'The database exists' else PRINT 'The database does not exist'
方法2:使用master数据库中的sysdatabases系统表
IF EXISTS(SELECT * FROM master..sysdatabases WHERE name=@testdb)
PRINT 'The database exists' else print 'The database does not exist'
方法3:使用sp_msforeachdb
--If you dont get a message, the database doesn't exist
DECLARE @sql varchar(1000)SET @sql='if ''?''='''+@ testdb+''' print ''the database exists'''EXEC sp_msforeachdb @sql
方法4:将sp_msforeachdb与information_schema.schemata一起使用
--If you dont get a message, the database doesn't exist
DECLARE @sql varchar(1000)
SET @sql='if exists(select * from ?.information_schema.schemata wherecatalog_name='''+@ testdb+''') print ''the database exists'''
EXEC sp_msforeachdb @sql
if exists (select * from master.sys.databases where name = '[enter name here]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.