日期:2014-05-18 浏览次数:21007 次
--判断[数据库]是不是存在
------------------------------
--数据库名:master
    if exists(select * from [master]..[sysdatabases] where name=N'master')
        select 'true'
    else
        select 'false'
------------------------
--判断[表]是不是存在。
--表名:[dbo].[a]
    use master
    go
    if exists (select * from dbo.sysobjects where (id = object_id(N'[dbo].[a]') and objectproperty/*对象财产*/ (id, N'IsUserTable') = 1))
        select 'true'
    else
        select 'false'
    go
--------------------------------------
--判断[临时表]是否存在
    if object_id('tempdb.dbo.T_P') is not null --tempdb.dbo.T_P  [临时表名]
        select '存在'
    else
        select '不存在'
-----------------------------------
--[存储过程] 是否存在 
--N'[dbo].[selectTabAById]': [存储过程名]
    if exists (
        select * from dbo.sysobjects where id = object_id(N'[dbo].[selectTabAById]') and OBJECTPROPERTY(id, N'IsProcedure') = 1
    )
        select 'true'
    else
        select 'false'
    go
create view 
------------------------------------------
--[视图] 是否存在 
-- [my_View_Users] :N'[dbo].[视图名]'
if exists (
    select * from dbo.sysobjects where id = object_id(N'[dbo].[my_View_Users]') and OBJECTPROPERTY(id, N'IsView') = 1
)
    select 'true';
else
    select 'false';
---------------------------------------------
-- 判断[函数名]是否存在
--函数名:[dbo].[DayOnly]
if exists (
    select * from dbo.sysobjects where id = object_id(N'[dbo].[DayOnly]') and xtype in (N'FN', N'IF', N'TF')
)
    select 'true';
else
    select 'false';
----------------------------------------------