亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程等)

 更新時間:2013年02月17日 14:27:27   作者:  
在sql server中,如何判斷sql server表是否存在呢?下面就將為您詳細介紹該方法,供您參考,希望對您加深理解sql server表能起到些許作用

sql server中如何判斷表或者數(shù)據(jù)庫的存在,但在實際使用中,需判斷Status狀態(tài)位:
其中某些狀態(tài)位可由用戶使用 sp_dboption(read only、dbo use only、single user 等)進行設(shè)置:

1 = autoclose;使用 sp_dboption 設(shè)置。 數(shù)據(jù)庫完全關(guān)閉,其資源在最后一個用戶注銷后釋放。
4 = select into/bulkcopy;使用 sp_dboption 設(shè)置。允許使用 Select INTO 語句和快速大容量復制。
8 = trunc. log on chkpt;使用 sp_dboption 設(shè)置。如果數(shù)據(jù)庫處于日志截斷模式,則檢查點將截斷日志中非活動的部分。只能為 master 數(shù)據(jù)庫設(shè)置此選項。16 = torn page detection,使用 sp_dboption 設(shè)置??梢詸z測殘缺頁。
32 = loading。
64 = pre recovery。
128 = recovering。
256 = not recovered。
512 = offline;使用sp_dboption 設(shè)置。數(shù)據(jù)庫將處于脫機狀態(tài)。
1024 = read only;使用 sp_dboption 設(shè)置。用戶僅能讀取數(shù)據(jù)庫中的數(shù)據(jù)而無法對其進行修改。
2048 = dbo use only;使用sp_dboption 設(shè)置。只有數(shù)據(jù)庫所有者可以使用數(shù)據(jù)庫。
4096 = single user;使用 sp_dboption 設(shè)置。每次只能有一個用戶訪問數(shù)據(jù)庫。
32768 = emergency mode。
4194304 = autoshrink。
1073741824 = cleanly shutdown。

可以同時打開多個位。

譬如:判斷一個數(shù)據(jù)庫是否offline
select * From master.dbo.sysdatabases where name='pubs' 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 '存在'
else
print '不存在'

SQL Server中判斷表中字段是否存在:
if exists(select * from syscolumns where name='colname1' and id=object_id('數(shù)據(jù)庫名.Owner.表名'))
print '存在'
else
print '不存在'
代表表tablename1中存在colname1字段
例:
select * from syscolumns where name='Test' and id=object_id('dbo.test')

Access中判斷表對象是否存在:
其實,Access數(shù)據(jù)庫也有系統(tǒng)表,存放有對象名
Select Count(*) AS Qty FROM MSysObjects Where ((MSysObjects.Name) Like '表名');

復制代碼 代碼如下:

庫是否存在
if exists(select * from master..sysdatabases where name=N'庫名')
print 'exists'
else
print 'not exists'
---------------
-- 判斷要創(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 默認值名稱
go
alter table 表名 drop column 列名
go
-----
--判斷要創(chuàng)建臨時表是否存在
If Object_Id('Tempdb.dbo.#Test') Is Not Null
Begin
print '存在'
End
Else
Begin
print '不存在'
End
---------------
-- 判斷要創(chuàng)建的存儲過程名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
-- 刪除存儲過程
drop procedure [dbo].[存儲過程名]
GO
---------------
-- 判斷要創(chuàng)建的視圖名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1)
-- 刪除視圖
drop view [dbo].[視圖名]
GO
---------------
-- 判斷要創(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)文章

最新評論