创建数据库、表、存储过程...前的判断

-创建数据库
if exists(select * from sysdatabases where name=N'master..test')
drop database test
create database test


--创建存储过程(法1)
if exists(select * from sysobjects where name=N'proc_name' and type='p')
drop proc proc_name
create proc proc_name
as
select * from table_name

--创建存储过程(法2)
if exists (select * from sysobjects where id=object_id(N'dbo.proc_name') and objectproperty(id,N'isprocedure')=1) --id是当前数据库中某个对象(表、存储过程.)的ID
--
or:if exists (select * from sysobjects where and objectproperty(object_id(N'dbo.proc_name'),N'isprocedure')=1)
drop proc proc_name
create proc proc_name
as
select * from table_name




--创建表(法1)
if exists(select * from sysobjects where name=N'table_name')
drop table table_name
create table table_name
(
_id int,
_name char(10)
)
insert into table_name select 1,'zhang' union all
select 2,'zhu' union all
select 3,'liuchunmei'

--创建表(法2)
if exists(select * from sysobjects where id=object_id(N'table_name') and objectproperty(id,N'istable')=1)
--or:if exists(select * from sysobjects where objectproperty(object_id(N'table_name'),N'istable')=1)
drop table table_name
create table table_name
(
_id int,
_name char(10)
)
insert into table_name select 1,'zhang' union all
select 2,'zhu' union all
select 3,'liuchunmei'

 OBJECTPROPERTY
返回当前数据库中对象的有关信息。

语法
OBJECTPROPERTY ( id , property )

参数
    id
    一个表达式,包含当前数据库中某个对象(表、存储过程…)的 ID。id 的数据类型是 int。

    Property

    一个表达式,包含针对由 id 指定的对象将要返回的信息。Property 可以是下面这些值中的一个。

    说明:除非加以注释,否则,如果 property 是无效的属性名,则返回 NULL。
   
    具体property属性请参考SQL Server 联机丛书

 

posted @ 2012-02-29 17:01  炒米饭  阅读(288)  评论(0编辑  收藏  举报