SQL Server中判断一个数据库是否存在的方法
来源:互联网 发布:sai软件win10下载 编辑:程序博客网 时间:2024/04/24 05:35
在SQL Server数据库编程时,常常需要判断一个数据库是否已经存在,如果不存在则创建此数据库。常用的方法有以下三种:
1. select * From master.dbo.sysdatabases where name='test_db'
如果不存在查询结果,则说明name所表示的数据库不存在
2. object_id('test_db')
如果无法获取对象ID(null),则说明此对象不存在;常用
if object_id('test_db') is null
或者
if (select object_id('test_db')) is null
3. db_id('test_db')
如果不能获取数据库ID,则说明name所表示的数据库不存在;实际上此种方法也是在sysdatabases中查找,并返回数据库的ID;常用
if db_id('test_db') is null
或者
if (select db_id('test_db')) is null
下面以ASP编程为例来说明,先判断数据库是否存在,如果不存在则创建此数据库:
' strSQL = "if not exists (select * From master.dbo.sysdatabases where name='test_db') "& _
' strSQL = "if object_id('test_db') is null "& _
strSQL = "use master if not exists (select db_id('test_db')) "& _
"create database test_db on primary (name='test_data',filename='d:/temp/test_db/test_db.mdf',size=5MB,maxsize=unlimited,filegrowth=1%) "& _
"log on (name='test_log',filename='d:/temp/test_db/test_log.ldf',size=1MB,maxsize=unlimited,filegrowth=1%) "
cn.Execute(strSQL)
1. select * From master.dbo.sysdatabases where name='test_db'
如果不存在查询结果,则说明name所表示的数据库不存在
2. object_id('test_db')
如果无法获取对象ID(null),则说明此对象不存在;常用
if object_id('test_db') is null
或者
if (select object_id('test_db')) is null
3. db_id('test_db')
如果不能获取数据库ID,则说明name所表示的数据库不存在;实际上此种方法也是在sysdatabases中查找,并返回数据库的ID;常用
if db_id('test_db') is null
或者
if (select db_id('test_db')) is null
下面以ASP编程为例来说明,先判断数据库是否存在,如果不存在则创建此数据库:
' strSQL = "if not exists (select * From master.dbo.sysdatabases where name='test_db') "& _
' strSQL = "if object_id('test_db') is null "& _
strSQL = "use master if not exists (select db_id('test_db')) "& _
"create database test_db on primary (name='test_data',filename='d:/temp/test_db/test_db.mdf',size=5MB,maxsize=unlimited,filegrowth=1%) "& _
"log on (name='test_log',filename='d:/temp/test_db/test_log.ldf',size=1MB,maxsize=unlimited,filegrowth=1%) "
cn.Execute(strSQL)
- SQL Server中判断一个数据库是否存在的方法
- SQL Server中判断一个数据库是否存在的方法
- SQL Server中判断一个数据库是否存在的方法
- SQL Server中如何判断一个数据库是否存在
- SQL Server中判断数据库是否存在
- SQL SERVER数据库判断对象是否存在的方法汇总
- SQL SERVER数据库判断对象是否存在的方法汇总
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- Sql Server中判断表或者数据库是否存在
- sql server如何判断数据库是否存在
- sql server 判断数据库、函数、表、存储过程、视图是否存在的方法
- Sql server中判断表是否存在
- SQL Server 中判断对象是否存在
- 在进行ASP调试时,可能出现的错误及尝试解决办法
- 一些不错的开发工具和开发相关工具推荐
- 网上收集资料(学习StrutsTestCase)转载版
- Proxy Authentication Required解决
- JspSmartUpload文档使用说明
- SQL Server中判断一个数据库是否存在的方法
- 网上收集资料(学习StrutsTestCase)转载版2
- ASP连接SQL Server数据库总结
- ns2仿真模型
- Windows消息
- .net 自用代码大全天天收藏(C#版)_01
- 在Insert Into...中采用变量的方法
- 进程与多进程管理的实现方法
- 解决HIbernate分页问题获取表总行数的小Bug