【转载】SQLServer中如何判断表或者数据库的存在
Sql Server中如何判断表或者数据库的存在作者:blue1000 出处:IT专家网论坛 【转载于】文章sql server中如何判断表或者数据库的存在,但在实际使用中,需判断Status状态位: 其中某些状态位可由用户使用 sp_dboption(read only、dbo use only、single user 等)进行设置: 1 = autoclose;使用 sp_dboption 设置。 数据库完全关闭,其资源在最后一个用户注销后释放。 4 = select into/bulkcopy;使用 sp_dboption 设置。允许使用 Select INTO 语句和快速大容量复制。 8 = trunc. log on chkpt;使用 sp_dboption 设置。如果数据库处于日志截断模式,则检查点将截断日志中非活动的部分。只能为 master 数据库设置此选项。16 = torn page detection,使用 sp_dboption 设置。可以检测残缺页。 32 = loading。 64 = pre recovery。 128 = recovering。 256 = not recovered。 512 = offline;使用sp_dboption 设置。数据库将处于脱机状态。 1024 = read only;使用 sp_dboption 设置。用户仅能读取数据库中的数据而无法对其进行修改。 2048 = dbo use only;使用sp_dboption 设置。只有数据库所有者可以使用数据库。 4096 = single user;使用 sp_dboption 设置。每次只能有一个用户访问数据库。 32768 = emergency mode。 4194304 = autoshrink。 1073741824 = cleanly shutdown。 可以同时打开多个位。 譬如:判断一个数据库是否offline select * From master.dbo.sysdatabases where name='pubs' and status<>512 SQL Server中判断表对象是否存在: select count(*) from sysobjects where id = object_id('数据库名.Owner.表名') if exists (select count(*) from sysobjects where id = object_id('数据库名.Owner.表名')) print '存在' else print '不存在' SQL Server中判断表中字段是否存在: if exists(select * from syscolumns where name='colname1' and id=object_id('数据库名.Owner.表名')) print '存在' else print '不存在' 代表表tablename1中存在colname1字段 例: select * from syscolumns where name='Test' and id=object_id('dbo.test') Access中判断表对象是否存在: 其实,Access数据库也有系统表,存放有对象名 Select Count(*) AS Qty FROM MSysObjects Where ((MSysObjects.Name) Like '表名'); 判断数据库和表是否存在 if not exists(select 1 From master.dbo.sysdatabases where name=N'JZKStarCfg')
******************************************************************* SQL Server中判断数据库是否存在: select * From master.dbo.sysdatabases where name='数据库名' 法(二): drop database 。。。 create 。。。 ?SQL Server中判断表对象是否存在: if exists? (select count(*) from sysobjects where id = object_id('数据库名.Owner.表名')) print '存在' SQL Server中判断表中字段是否存在: (select * from syscolumns where name='colname1' and id=object_id('数据库名.Owner.表名')) ? SQL Server中判断存储过程或视图是否存在: ? if object_id('视图或存储过程名')? is not null create proc/view? 。。。 或 if Exists(select * from sysobjects where name='视图或存储过程名'? AND??type? =? 'P/V') create?proc/view ?。。。? //*************************************************** ? /// <summary> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |