1 頁 (共 1 頁)

MSSQL中判定資料表是否存在及檢驗物件屬性的方法

發表於 : 2008年 11月 28日, 17:04
tim
判定資料表是否存在:

if exists (select * from sysobjects where id = object_id(N'[dbo].[mytable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)



if exists (select * from sysobjects where id = object_id('mytable') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

判定物件屬性:

以下範例測試 authors 是否為資料表。

IF OBJECTPROPERTY ( object_id('authors'),'ISTABLE') = 1
print 'Authors is a table'

ELSE IF OBJECTPROPERTY ( object_id('authors'),'ISTABLE') = 0
print 'Authors is not a table'

ELSE IF OBJECTPROPERTY ( object_id('authors'),'ISTABLE') IS NULL
print 'ERROR: Authors is not an object'