sql 判斷函數(shù)、存儲(chǔ)過程是否存在

字號(hào):


    介紹sql下用了判斷各種資源是否存在的代碼,需要的朋友可以參考下,希望對(duì)您學(xué)習(xí)sql的函數(shù)及數(shù)據(jù)庫(kù)能夠有所幫助。
    1
    -- 庫(kù)是否存在
    2
    if exists(select * from master..sysdatabases where name=N'庫(kù)名')
    3
    print 'exists'
    4
    else
    5
    print 'not exists'
    1
    -- 判斷要?jiǎng)?chuàng)建的表名是否存在
    2
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    3
    -- 刪除表
    4
    drop table [dbo].[表名]
    5
    GO
    1
    --判斷要?jiǎng)?chuàng)建臨時(shí)表是否存在
    2
    If Object_Id('Tempdb.dbo.#Test') Is Not Null
    3
    Begin
    4
    print '存在'
    5
    End
    6
    Else
    7
    Begin
    8
    print '不存在'
    9
    End
    01
    -- 判斷要?jiǎng)?chuàng)建的存儲(chǔ)過程名是否存在
    02
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲(chǔ)過程名]') andOBJECTPROPERTY(id, N'IsProcedure') = 1)
    03
    -- 刪除存儲(chǔ)過程
    04
    drop procedure [dbo].[存儲(chǔ)過程名]
    05
    GO
    06
    -- 判斷要?jiǎng)?chuàng)建的視圖名是否存在
    07
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1)
    08
    -- 刪除視圖
    09
    drop view [dbo].[視圖名]
    10
    GO
    11
    -- 判斷要?jiǎng)?chuàng)建的函數(shù)名是否存在
    12
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函數(shù)名]') and xtype in (N'FN', N'IF', N'TF'))
    13
    -- 刪除函數(shù)
    14
    drop function [dbo].[函數(shù)名]
    15
    GO
    16
    if col_length('表名', '列名') is null
    17
    print '不存在'
    18
    select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'