当前位置: 代码迷 >> Sql Server >> 求指导下面几个语句的意思解决方法
  详细解决方案

求指导下面几个语句的意思解决方法

热度:72   发布时间:2016-04-27 14:50:41.0
求指导下面几个语句的意思
刚学数据库,求指导下面几个语句的含义,写上注释。谢谢了
if exists(select * from sysobjects where id=object_id(N'Test1') 
and OBJECTPROPERTY(id, N'IsUserTable') = 1)

------解决方案--------------------
SQL code
if exists(select * from sysobjects where id=object_id(N'Test1')  and OBJECTPROPERTY(id, N'IsUserTable') = 1)--你这个不全,应该还有一句drop table Test1
------解决方案--------------------
SQL code
if exists(select * from sysobjects where id=object_id(N'Test1')  and OBJECTPROPERTY(id, N'IsUserTable') = 1)--表示在数据库中存在TEST1的表 IsUserTable=1表示这个表还是用户表,而不是系统表
------解决方案--------------------
查询当前数据库下,是否有一个叫"Test1"的用户表,

如果有,此if条件则成立.
------解决方案--------------------
创建表前判断下是否已经存在此表,如存在,则删除,再建。
------解决方案--------------------
SQL code
---建表语句if object_id('Test1') is not null drop table Test1gocreate table Test1( id int identity(1,1) primary key, startTime datetime, endTime datetime, 人员 nvarchar(5))
------解决方案--------------------
if exists(select * from sysobjects where id=object_id(N'Test1')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)

exists是判断子查询是否返回记录

子查中的

select * from sysobjects where id=object_id(N'Test1')
and OBJECTPROPERTY(id, N'IsUserTable') = 1
查找到id和表属性是用户表的记录
  相关解决方案