SQL Server中如何判断一个数据库是否存在
来源:互联网 发布:英法百年战争知乎 编辑:程序博客网 时间:2024/04/25 06:34
在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)
转自:http://blog.csdn.net/denlee/article/details/1591780
- 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 中判断对象是否存在
- Android之内存泄漏调试学习与总结
- Android自定义(三)实现圆盘的百分比设置
- 学java好难啊
- MAC下终端sqlite3简单使用
- AdapterViewFlipper
- SQL Server中如何判断一个数据库是否存在
- Android应用混淆问题记录
- hibernate缓存:一级缓存和二级缓存
- Linux--内存结构
- ubuntu 搭建ftp服务器
- C++ 输出乘法表
- Linux驱动:动态加载hello world模块
- IOS沙盒中的文件夹及其作用
- Jackson的json转换和Jackson Annotations 基本用法