SQL Server中判断一个数据库是否存在的方法
来源:互联网 发布:淘宝代运营销售怎么样 编辑:程序博客网 时间:2024/04/25 01:17
在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 中判断对象是否存在
- AptitudeFramework 平台概述
- C#向Excel报表中插入图片的2种方法
- 嵌入式Linux开发大会的讲演资料下载
- tapestry框架自定义组件开发
- listbox控件的一些操作
- SQL Server中判断一个数据库是否存在的方法
- jquery插件jCarouselLite实现图片无缝滚动
- 真正解决jspSmartUpload组件上传下载文件时中文乱码问题!!!
- 转载—— WinCE中中断的处理过程(驱动开发人员角度)
- 关于qApp->installEventFilter( this )
- Java解析XML文档——dom解析xml
- 10家视听网站关停
- fckeditor-java-2.4.1 repack
- 静态分析EnumProcessModules