判斷sql server表是否存在的方法
在sql server中,如何判斷sql server表是否存在呢?下面就將為您詳細(xì)介紹該方法,供您參考,希望對(duì)您加深理解sql server表能起到些許作用。
sql server中如何判斷表或者數(shù)據(jù)庫(kù)的存在,但在實(shí)際使用中,需判斷Status狀態(tài)位:
其中某些狀態(tài)位可由用戶(hù)使用 sp_dboption(read only、dbo use only、single user 等)進(jìn)行設(shè)置:
1 = autoclose;使用 sp_dboption 設(shè)置。 數(shù)據(jù)庫(kù)完全關(guān)閉,其資源在***一個(gè)用戶(hù)注銷(xiāo)后釋放。
4 = select into/bulkcopy;使用 sp_dboption 設(shè)置。允許使用 Select INTO 語(yǔ)句和快速大容量復(fù)制。
8 = trunc. log on chkpt;使用 sp_dboption 設(shè)置。如果數(shù)據(jù)庫(kù)處于日志截?cái)嗄J?,則檢查點(diǎn)將截?cái)嗳罩局蟹腔顒?dòng)的部分。只能為 master 數(shù)據(jù)庫(kù)設(shè)置此選項(xiàng)。16 = torn page detection,使用 sp_dboption 設(shè)置。可以檢測(cè)殘缺頁(yè)。
32 = loading。
64 = pre recovery。
128 = recovering。
256 = not recovered。
512 = offline;使用sp_dboption 設(shè)置。數(shù)據(jù)庫(kù)將處于脫機(jī)狀態(tài)。
1024 = read only;使用 sp_dboption 設(shè)置。用戶(hù)僅能讀取數(shù)據(jù)庫(kù)中的數(shù)據(jù)而無(wú)法對(duì)其進(jìn)行修改。
2048 = dbo use only;使用sp_dboption 設(shè)置。只有數(shù)據(jù)庫(kù)所有者可以使用數(shù)據(jù)庫(kù)。
4096 = single user;使用 sp_dboption 設(shè)置。每次只能有一個(gè)用戶(hù)訪問(wèn)數(shù)據(jù)庫(kù)。
32768 = emergency mode。
4194304 = autoshrink。
1073741824 = cleanly shutdown。
可以同時(shí)打開(kāi)多個(gè)位。
譬如:判斷一個(gè)數(shù)據(jù)庫(kù)是否offline
select * From master.dbo.sysdatabases where name='pubs' and status<>512
SQL Server中判斷表對(duì)象是否存在:
select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫(kù)名.Owner.表名')
if exists
(select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫(kù)名.Owner.表名'))
print '存在'
else
print '不存在'
SQL Server中判斷表中字段是否存在:
if exists(select * from syscolumns where name='colname1' and id=object_id('數(shù)據(jù)庫(kù)名.Owner.表名'))
print '存在'
else
print '不存在'
代表表tablename1中存在colname1字段
例:
select * from syscolumns where name='Test' and id=object_id('dbo.test')
Access中判斷表對(duì)象是否存在:
其實(shí),Access數(shù)據(jù)庫(kù)也有系統(tǒng)表,存放有對(duì)象名
Select Count(*) AS Qty FROM MSysObjects Where ((MSysObjects.Name) Like '表名');
【編輯推薦】
教您SQL SERVER查詢(xún)時(shí)間條件式寫(xiě)法