SQL Server中判断一个数据库是否存在的方法
来源:互联网 发布:c 和java的区别 编辑:程序博客网 时间:2024/04/26 22:45
SQL Server中判断一个数据库是否存在的方法
在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 中判断对象是否存在
- C++内存泄漏检查心得
- J2EE开发初步,MyEclipse+SQLServer2000插入、删除、更新、查询步骤+代码(使用PreparedStatement对象)
- GDB远程调试(linux pc + target)
- [linux]syslog使用
- Java程序设计的基本概念
- SQL Server中判断一个数据库是否存在的方法
- 装SQL server2005的烦恼
- debian(lenny)网络查看软件和命令
- linux中动态链接库的创建与使用
- C#获取当前路径的方法集合
- 技术就是一层“纸”,一点就破
- 学习SPI与SD卡必读
- [linux]gstreamer笔记
- 很精彩的linux命令