sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程
導(dǎo)讀:1建站知識在sql server中,如何判斷sql server表是否存在呢?下面就將為您詳細(xì)介紹該方法,供您參考,希望對您加深理解sql serv企業(yè)網(wǎng)站建設(shè)seo網(wǎng)站優(yōu)化軟件。
sql server中如何判斷表或者數(shù)據(jù)庫的存在,但在實(shí)際使用中,需判斷Status狀態(tài)位:其中某些狀態(tài)位可由用戶使用 sp_dboption(read only、dbo use only、single user 等)進(jìn)行設(shè)置:
1 = autoclose;使用 sp_dboption 設(shè)置。 數(shù)據(jù)庫完全關(guān)閉,其資源在最后一個(gè)用戶注銷后釋放。4 = select into/bulkcopy;使用 sp_dboption 設(shè)置。允許使用 Select INTO 語句和快速大容量復(fù)制。8 = trunc. log on chkpt;使用 sp_dboption 設(shè)置。如果數(shù)據(jù)庫處于日志截?cái)嗄J剑瑒t檢查點(diǎn)將截?cái)嗳罩局蟹腔顒拥牟糠帧V荒転?master 數(shù)據(jù)庫設(shè)置此選項(xiàng)。16 = torn page detection,使用 sp_dboption 設(shè)置。可以檢測殘缺頁。32 = loading。64 = pre recovery。128 = recovering。256 = not recovered。512 = offline;使用sp_dboption 設(shè)置。數(shù)據(jù)庫將處于脫機(jī)狀態(tài)。1024 = read only;使用 sp_dboption 設(shè)置。用戶僅能讀取數(shù)據(jù)庫中的數(shù)據(jù)而無法對其進(jìn)行修改。2048 = dbo use only;使用sp_dboption 設(shè)置。只有數(shù)據(jù)庫所有者可以使用數(shù)據(jù)庫。4096 = single user;使用 sp_dboption 設(shè)置。每次只能有一個(gè)用戶訪問數(shù)據(jù)庫。32768 = emergency mode。4194304 = autoshrink。1073741824 = cleanly shutdown。
可以同時(shí)打開多個(gè)位。
譬如:判斷一個(gè)數(shù)據(jù)庫是否offlineselect * From master.dbo.sysdatabases where and status<>512
SQL Server中判斷表對象是否存在:select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫名.Owner.表名')
if exists(select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫名.Owner.表名'))print '存在'elseprint '不存在'
SQL Server中判斷表中字段是否存在:if exists(select * from syscolumns where and id=object_id('數(shù)據(jù)庫名.Owner.表名'))print '存在網(wǎng)站建設(shè)教程'elseprint '不存在'代表表tablename1中存在colname1字段例:sseo網(wǎng)站優(yōu)化培訓(xùn)elect * from syscolumns where and id=object_id('dbo.test')
Access中判斷表對象是否存在:其實(shí),Access數(shù)據(jù)庫也有系統(tǒng)表,存放有對象名Select Count(*) AS Qty FROM MSysObjects Where ((MSysObjects.Name) Like '表名');
復(fù)制代碼 代碼如下:
庫是否存在 if exists(select * from master..sysdatabases where name=N'庫名') print 'exists'elseprint 'not exists'--------------- -- 判斷要?jiǎng)?chuàng)建的表名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) -- 刪除表 drop table [dbo].[表名] GO --------------- -----列是否存在 IF COL_LENGTH( '表名','列名') IS NULL PRINT 'not exists'ELSE PRINT 'exists'alter table 表名 drop constraint 默認(rèn)值名稱 go alter table 表名 drop column 列名 go ----- --判斷要?jiǎng)?chuàng)建臨時(shí)表是否存在 If Object_Id('Tempdb.dbo.#Test') Is Not Nulseo網(wǎng)站優(yōu)化lBeginprint '存在'EndElseBeginprint '不存在'End--------------- -- 判斷要?jiǎng)?chuàng)建的存儲過程名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) -- 刪除存儲過程 drop procedure [dbo].[存儲過程名] GO --------------- -- 判斷要?jiǎng)?chuàng)建的視圖名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1) -- 刪除視圖 drop view [dbo].[視圖名] GO --------------- -- 判斷要?jiǎng)?chuàng)建的函數(shù)名是否存在 if exists (select * from sysobjects where xtype='fn' and) if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函數(shù)名]') and xtype in (N'FN', N'IF', N'TF')) -- 刪除函數(shù) drop function [dbo].[函數(shù)名] GO if col_length('表名', '列名') is nullprint '不存在'select 1 from sysobjects where id in (select id from syscolumns where) and
聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-23 ,文章sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程主要講述存儲過程,函數(shù),sqlserver 各種判斷是否存在(表名、網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請保留鏈接: http://www.bifwcx.com/article/web_6067.html
為你推薦與sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程相關(guān)的文章
-
通王TWCMS 2.0.3網(wǎng)站模板程序下載
(126)人喜歡 2024-01-15 -
Windows官方原版在哪里下載
(175)人喜歡 2024-01-15 -
WordPress網(wǎng)站模板發(fā)帖標(biāo)題顏色設(shè)置
(131)人喜歡 2024-01-07 -
修改discuz論壇帖子標(biāo)題80字符的長度限制
(249)人喜歡 2024-01-07 -
wordpress程序調(diào)用不帶超鏈接的Tag標(biāo)簽
(234)人喜歡 2024-01-05 -
網(wǎng)站在不同時(shí)期需調(diào)整內(nèi)容更新的方向
(112)人喜歡 2023-08-12