sql server判斷表存在

2022-02-22 22:57:59 字數 1246 閱讀 2868

在建立表、更改表結構、刪除表或對錶進行什麼操作之前,乙個比較嚴謹的做法是先判斷該錶是否已經存在。

在sql server中判斷乙個表是否存在,有兩個方法,下面以diso表為例。

方法1

if

exists(select

top1

1from sysobjects where id =

object_id(n'

diso

') and xtype ='u

')print

'表diso存在

'else

print

'表diso不存在

'

原理是查詢【sysobjects】這張系統表,該錶儲存了所有物件資訊,既然是所有物件,自然包括表的資訊,其中xtype為【u表示為使用者表。

方法2

if

object_id(n'

diso

', n'

u') is

notnull

print

'表diso存在

'else

print

'表diso不存在

'

臨時表

前面都是判斷普通表,如果是判斷臨時表的話,則需要在臨時表前加上【tempdb..】字首,指明這是乙個臨時表。

if

exists(select

top1

1from sysobjects where id =

object_id(n'

tempdb..#diso

') and xtype ='u

')print

'表#diso存在

'else

print

'表#diso不存在

'

if

object_id(n'

tempdb..#diso

', n'

u') is

notnull

print

'表diso存在

'else

print

'表diso不存在

'

臨時表實際上還是乙個表,只不過查詢的時候和實體表還是有點區別。

"去走自己的路,贏要贏得理所當然,輸也要輸得清清楚楚。"

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...

SQL Server 判斷表是否存在

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中判斷表是否存在

在sqlserver 應該說在目前所有資料庫產品 中建立乙個資源如表,檢視,儲存過程中都要判斷與建立的資源是否已經存在 在sqlserver中一般可通過查詢sys.objects系統表來得知結果,不過可以有更方便的方法 如下 if object id tb table is not null pri...