SQL Server 判斷表是否存在

2021-06-25 18:55:28 字數 1066 閱讀 2739

1、判斷資料表是否存在

方法一:

use yourdb;

goif object_id(n'tablename',n'u') is not null

print '存在'

else

print '不存在'

例如:use fireweb;

goif object_id(n'temp_tbl',n'u') is not null

print '存在'

else

print '不存在'

方法二:

use [例項名]

go if exists  (select  * from dbo.sysobjects where id = object_id(n'[表名]') and objectproperty(id, 'istable') = 1)

print '存在'

else

print'不存在'

例如:use fireweb;

goif exists  (select  * from dbo.sysobjects where id = object_id(n'temp_tbl') and objectproperty(id, 'istable') = 1)

print '存在'

else

print'不存在'

2、臨時表是否存在:

方法一:

use fireweb;

goif exists(select * from tempdb..sysobjects where id=object_id('tempdb..##temp_tbl'))

print '存在'

else

print'不存在'

方法二:

use fireweb;

goif exists (select * from tempdb.dbo.sysobjects where id = object_id(n'tempdb..#temp_tbl') and type='u')

print '存在'

else

print'不存在'

sqlserver中判斷表或臨時表是否存在

1 判斷資料表是否存在 方法一 use yourdb goif object id n tablename n u is not null print 存在 else print 不存在 例如 use fireweb goif object id n temp tbl n u is not null...

sqlserver中判斷表或臨時表是否存在

方法一 use yourdb goif object id n tablename n u is not null print 存在 else print 不存在 例如 use fireweb goif object id n temp tbl n u is not null print 存在 el...

sqlserver中判斷表或臨時表是否存在

1 判斷資料表是否存在 方法一 use yourdb goif object id n tablename n u is not null print 存在 else print 不存在 例如 use fireweb goif object id n temp tbl n u is not null...