判斷sqlserver對象是否存在

來源:互聯網
上載者:User

標籤:

--查看對象是否已經存在  --資料庫是否存在     --if exists (select * from sys.databases where name = ’資料庫名’)    --  drop database [資料庫名]        if exists( select * from sys.databases where name = ‘FGM_POS‘ print ‘存在‘  --drop database [資料庫名]     --表是否存在     --if exists (select * from sysobjects where id = object_id(N’[表名]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)    --  drop table [表名]        if exists ( select * from sysobjects where id =OBJECT_ID(N ‘[FGM_bt_ePlnMain]‘ ) and OBJECTPROPERTY(id,N ‘IsUserTable‘ )=1)  print ‘存在‘  --drop table [表名]  --或  if exists ( select * from sysobjects where id =OBJECT_ID(N ‘dbo.FGM_bt_ePlnMain‘ ) and OBJECTPROPERTY(id,N ‘IsUserTable‘ )=1)  print ‘存在‘     --預存程序是否存在     --if exists (select * from sysobjects where id = object_id(N’[預存程序名]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)    --  drop procedure [預存程序名]         if exists ( select * from sysobjects where id =OBJECT_ID(N ‘[FGM_sp_SyncePlnMain]‘ ) and OBJECTPROPERTY(id,N ‘IsProcedure‘ )=1)  print ‘存在‘  --或  if exists ( select * from sysobjects where id =OBJECT_ID(N ‘dbo.FGM_sp_SyncePlnMain‘ ) and OBJECTPROPERTY(id,N ‘IsProcedure‘ )=1)  print ‘存在‘     --暫存資料表是否存在     --if object_id(’tempdb..#暫存資料表名’) is not null      --  drop table #暫存資料表名         select * into # temp from dbo.FGM_bt_ConsumInfoDetail_B  if OBJECT_ID ( ‘tempdb..#temp‘ ) is not null  print ‘存在‘  --或者  if exists( select * from tempdb.dbo.sysobjects where id=OBJECT_ID(N ‘tempdb..#temp‘ ) and type= ‘U‘ print ‘存在‘     --視圖是否存在  ----SQL Server 2000     --IF EXISTS (SELECT * FROM sysviews WHERE object_id = ‘[dbo].[視圖名]‘    ----SQL Server 2005     --IF EXISTS (SELECT * FROM sys.views WHERE object_id = ‘[dbo].[視圖名]‘  --SQL Server 2000  --IF EXISTS (SELECT * FROM sysviews WHERE object_id = ‘[dbo].[視圖名]’  ----SQL Server 2005  --IF EXISTS (SELECT * FROM sys.views WHERE object_id = ’[dbo].[視圖名]’  ----SQL Server 2008  if exists ( select * from sysobjects where id=OBJECT_ID(N‘[dc_adplan] ‘) and type=‘ V ‘)  print ‘ 存在 ‘  --或  if exists (select * from sysobjects where id=OBJECT_ID(N‘ dbo.dc_adplan ‘) and type=‘ V ‘)  print ‘ 存在 ‘  --drop view dbo.dc_adplan     --函數是否存在       --if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[函數名]’) and xtype in (N’FN’, N’IF’, N’TF’))        -- drop function [dbo].[函數名]         if exists (select * from dbo.sysobjects where id=OBJECT_ID (N‘ dbo.GetResourceName ‘)and xtype in (N‘ FN ‘,N‘ IF ‘,N‘ TF ‘))  print ‘ 存在 ‘  --或  if exists (select * from dbo.sysobjects where id=OBJECT_ID (N‘ [GetResourceName] ‘)and xtype in (N‘ FN ‘,N‘ IF ‘,N‘ TF ‘))  print ‘ 存在 ‘  --drop function dbo.GetResourceName     --列是否存在     --if exists(select * from syscolumns where id=object_id(’表名’) and name=’列名’)    --  alter table 表名 drop column 列名      if exists(select * from syscolumns where id =OBJECT_ID(‘ FGM_bt_ePlnMain ‘) and name=‘ ePlnName ‘)  print ‘ 存在 ‘  --alter table 表名 drop column 列名     --判斷列是否自自增列     --if columnproperty(object_id(‘ table ‘),‘ col’,’IsIdentity’)=1    --  print ‘自增列‘    --else    --  print ‘不是自增列‘       if COLUMNPROPERTY(object_id ( ‘FGM_bt_ePlnMain‘ ), ‘ePlnID‘ , ‘IsIdentity‘ )=1  print ‘自增列‘  else  print ‘非‘        --查看資料庫中對象  --select * from sys.sysobjects where name=‘對象名‘    select * from sys.sysobjects where name = ‘FGM_bt_ePlnMain‘    --擷取使用者建立的對象資訊     SELECT [ name ],[id],crdate FROM sysobjects where xtype= ‘U‘ /*    xtype 的表示參數類型,通常包括如下這些    C = CHECK 條件約束    D = 預設值或 DEFAULT 約束    F = FOREIGN KEY 約束    L = 日誌    FN = 純量涵式    IF = 內嵌表函數    P = 預存程序    PK = PRIMARY KEY 約束(類型是 K)    RF = 複製篩選預存程序    S = 系統資料表    TF = 表函數    TR = 觸發器    U = 使用者表    UQ = UNIQUE 約束(類型是 K)    V = 視圖    X = 擴充預存程序    */     本文出自 “Don‘t Know” 部落格,請務必保留此出處http://3941954.blog.51cto.com/3931954/774083

判斷sqlserver對象是否存在

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.