SQL语句,查询数据库里是否存在某个表
2016-06-14 09:59
531 查看
http://blog.csdn.net/jeffrey9061/article/details/8235416
select count(1) from sys.objects where name = '表名'
select ObjectProperty(Object_ID( '表名'),'IsUserTable')
这两个SQL语句都是查询数据库里头是否存在某个表的语句,以后都是查询表里某个列,第一次查询数据库是否有某个表,原来是这么弄的,收录一下。
SQL Server中判断数据库是否存在:
法(一):
select * From master.dbo.sysdatabases where name='数据库名'
法(二):
if db_id('数据库名') is not null
drop database 。。。
go
create 。。。
SQL Server中判断表对象是否存在:
select count(*) from sysobjects where id = object_id('数据库名.Owner.表名')
if exists
(select count(*) from sysobjects where id = object_id('数据库名.Owner.表名'))
print '存在'
else
print '不存在'
SQL Server中判断表中字段是否存在:
if exists
(select * from syscolumns where name='colname1' and id=object_id('数据库名.Owner.表名'))
print '存在'
else
print '不存在'
(代表表tablename1中存在colname1字段 )
例:
select * from syscolumns where name='Test' and id=object_id('dbo.test')
SQL Server中判断存储过程或视图是否存在:
if object_id('视图或存储过程名') is not null
drop proc/view 。。。
go
create proc/view 。。。
或
if Exists(select * from sysobjects where name='视图或存储过程名' AND type = 'P/V')
drop proc/view 。。。
go
create proc/view 。。。
select count(1) from sys.objects where name = '表名'
select ObjectProperty(Object_ID( '表名'),'IsUserTable')
这两个SQL语句都是查询数据库里头是否存在某个表的语句,以后都是查询表里某个列,第一次查询数据库是否有某个表,原来是这么弄的,收录一下。
SQL Server中判断数据库是否存在:
法(一):
select * From master.dbo.sysdatabases where name='数据库名'
法(二):
if db_id('数据库名') is not null
drop database 。。。
go
create 。。。
SQL Server中判断表对象是否存在:
select count(*) from sysobjects where id = object_id('数据库名.Owner.表名')
if exists
(select count(*) from sysobjects where id = object_id('数据库名.Owner.表名'))
print '存在'
else
print '不存在'
SQL Server中判断表中字段是否存在:
if exists
(select * from syscolumns where name='colname1' and id=object_id('数据库名.Owner.表名'))
print '存在'
else
print '不存在'
(代表表tablename1中存在colname1字段 )
例:
select * from syscolumns where name='Test' and id=object_id('dbo.test')
SQL Server中判断存储过程或视图是否存在:
if object_id('视图或存储过程名') is not null
drop proc/view 。。。
go
create proc/view 。。。
或
if Exists(select * from sysobjects where name='视图或存储过程名' AND type = 'P/V')
drop proc/view 。。。
go
create proc/view 。。。
相关文章推荐
- Oracle 分区表的新增、修改、删除、合并。普通表转分区表方法
- redis系列--服务级别命令
- oracle编译死锁问题解决
- 主机访问虚拟机oracle数据库
- mysql中文乱码问题——命令行设置编码格式
- MySQL库中表名忽略大小写设置的影响
- mysql 优化
- mongodb查询文档
- Windows下安装并设置Redis
- Mysql数据库insert into select 单表插入常量
- Hibernate配置各种数据源详解
- window的PHP开发(wamp)下安装redis扩展实录
- org.gjt.mm.mysql.Driver和com.mysql.jdbc.Driver有啥区别?
- 常用的数据统计Sql 总结(转)
- 在oracle表中插入空字段和null测试
- Linux环境下使用定时器通过sqlldr向oracle数据库导入数据
- oracle alter session和alter system的参数详解
- MEAN全栈开发学习笔记1===>栈的安装
- Oracle SQL PLUS 执行SQL脚本文件是否执行后续SQL
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure