+ -
当前位置:首页 → 问答吧 → sqlserver能不能用sql语句来判断数据库里有多少表,判断某个表是否存在呢?有没有什么方法呢?

sqlserver能不能用sql语句来判断数据库里有多少表,判断某个表是否存在呢?有没有什么方法呢?

时间:2011-11-28

来源:互联网

sqlserver能不能用sql语句来判断数据库里有多少表,判断某个表是否存在呢?如果不能请大侠们给点好方法!
非常感谢@!!!

作者: ciqiqi   发布时间: 2011-11-28

sys.tables

作者: sql_sf   发布时间: 2011-11-28

SQL code
if exists (select 1 from sysobjects where name = '表名' and xtype = 'p')
print 存在

作者: ssp2009   发布时间: 2011-11-28

SQL code
select count(1) from sysobjects where xtype='U'

if object_id('表名') is not null
print ''
else 
print ''

作者: roy_88   发布时间: 2011-11-28

SQL code

--1sql语句来判断数据库里有多少表
select count(*) from sys.tables

--2
if object_id('数据库.架构.表') is not null
print  '存在'
else
print '不存在'

作者: Beirut   发布时间: 2011-11-28

INFORMATION_SCHEMA.TABLES/sys.objects視圖--都可以

作者: roy_88   发布时间: 2011-11-28

引用 2 楼 ssp2009 的回复:
SQL code
if exists (select 1 from sysobjects where name = '表名' and xtype = 'p')
print 存在
p改为U,p是存储过程。。。

作者: ssp2009   发布时间: 2011-11-28

前三楼的不是妹妹的都是坏银。

作者: Beirut   发布时间: 2011-11-28

引用 6 楼 ssp2009 的回复:
引用 2 楼 ssp2009 的回复:
SQL code
if exists (select 1 from sysobjects where name = '表名' and xtype = 'p')
print 存在
p改为U,p是存储过程。。。

嗯,sysobjects 是系统表吗?(本人对于数据库还很懵懂,所以不要闲我小白)

作者: ciqiqi   发布时间: 2011-11-28

是系统表

作者: ssp2009   发布时间: 2011-11-28

[code=SQL][/code]
SELECT * FROM SYSOBJECTS WHERE XTYPE='U'

作者: Sunnynanbing   发布时间: 2011-11-28

引用 4 楼 beirut 的回复:
SQL code

--1sql语句来判断数据库里有多少表
select count(*) from sys.tables

--2
if object_id('数据库.架构.表') is not null
print '存在'
else
print '不存在'

我对于这方面的知识很欠缺,请问有没有什么资料可以推荐给我的,非常感谢!

作者: ciqiqi   发布时间: 2011-11-28

引用 3 楼 roy_88 的回复:
SQL code
select count(1) from sysobjects where xtype='U'

if object_id('表名') is not null
print '有'
else
print '沒'

我对于这方面的知识很欠缺,请问有没有什么资料可以推荐给我的,非常感谢!

作者: ciqiqi   发布时间: 2011-11-28

SQL code
if object_id('表名') is not null
print ''
else 
print ''

作者: fredrickhu   发布时间: 2011-11-28

引用 12 楼 ciqiqi 的回复:
引用 3 楼 roy_88 的回复:
SQL code
select count(1) from sysobjects where xtype='U'

if object_id('表名') is not null
print '有'
else
print '沒'

我对于这方面的知识很欠缺,请问有没有什么资料可以推荐给我的,非常感谢!

你想要一些什么资料 我可以贴一点点给你。

作者: fredrickhu   发布时间: 2011-11-28