sqlserver 各種判斷是否存在(表名、函數(shù)、存儲(chǔ)過(guò)程等)
sql server中如何判斷表或者數(shù)據(jù)庫(kù)的存在,但在實(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ù)庫(kù)完全關(guān)閉,其資源在最后一個(gè)用戶注銷(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è)置。用戶僅能讀取數(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è)用戶訪問(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 '表名');
庫(kù)是否存在
if exists(select * from master..sysdatabases where name=N'庫(kù)名')
print 'exists'
else
print '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 Null
Begin
print '存在'
End
Else
Begin
print '不存在'
End
---------------
-- 判斷要?jiǎng)?chuàng)建的存儲(chǔ)過(guò)程名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲(chǔ)過(guò)程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
-- 刪除存儲(chǔ)過(guò)程
drop procedure [dbo].[存儲(chǔ)過(guò)程名]
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 name='函數(shù)名')
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 null
print '不存在'
select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'
相關(guān)文章
SQL Server 觸發(fā)器 表的特定字段更新時(shí),觸發(fā)Update觸發(fā)器
另外再補(bǔ)充一句:insert和update的數(shù)據(jù)都會(huì)保存在臨時(shí)表中,所以使用inserted可以取出這些數(shù)據(jù),刪除時(shí)使用deleted可以取出被刪除的數(shù)據(jù)2009-08-08存儲(chǔ)過(guò)程解密(破解函數(shù),過(guò)程,觸發(fā)器,視圖.僅限于SQLSERVER2000)
解密指定存儲(chǔ)過(guò)程 exec sp_decrypt '存儲(chǔ)過(guò)程名'2009-05-05SQLServer中字符串左對(duì)齊或右對(duì)齊顯示的sql語(yǔ)句
在顯示數(shù)據(jù)時(shí)需要對(duì)數(shù)據(jù)進(jìn)行美觀化顯示。如左對(duì)齊,右對(duì)齊2012-05-05asp.net中如何調(diào)用sql存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)
使用sql存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè),在網(wǎng)上能找到好多種解決方案,但是如何用asp.net后臺(tái)調(diào)用呢,通過(guò)本篇文章小編給大家詳解asp.net中如何調(diào)用sql存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè),有需要的朋友可以來(lái)參考下2015-08-08sqlserver isnull在數(shù)據(jù)庫(kù)查詢中的應(yīng)用
isnull在數(shù)據(jù)庫(kù)查詢中的應(yīng)用,特別是再語(yǔ)句連接的時(shí)候需要用到2011-11-11