標籤:
sql判斷預存程序是否存在判斷資料庫教程是否存在Sql代碼if exists (select * from sys.databases where name = ’資料庫名’) drop database [資料庫名] if exists (select * from sys.databases where name = ’資料庫名’) drop database [資料庫名]判斷表是否存在Sql代碼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’[表名]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1) drop table [表名]判斷預存程序是否存在Sql代碼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’[預存程序名]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1) drop procedure [預存程序名]判斷暫存資料表是否存在Sql代碼if object_id(’tempdb..#暫存資料表名’) is not null drop table #暫存資料表名 if object_id(’tempdb..#暫存資料表名’) is not null drop table #暫存資料表名判斷視圖是否存在Sql代碼--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 2000IF EXISTS (SELECT * FROM sysviews WHERE object_id = ’[dbo].[視圖名]’--SQL Server 2005IF EXISTS (SELECT * FROM sys.views WHERE object_id = ’[dbo].[視圖名]’判斷函數是否存在Sql代碼-- 判斷要建立的函數名是否存在 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].[函數名]’) and xtype in (N’FN’, N’IF’, N’TF’)) drop function [dbo].[函數名] 擷取使用者建立的對象資訊Sql代碼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 = 擴充預存程序 */ 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 = 擴充預存程序*/判斷列是否存在Sql代碼if exists(select * from syscolumns where id=object_id(’表名’) and name=’列名’) alter table 表名 drop column 列名 if exists(select * from syscolumns where id=object_id(’表名’) and name=’列名’) alter table 表名 drop column 列名判斷列是否自增列Sql代碼if columnproperty(object_id(’table’),’col’,’IsIdentity’)=1 print ’自增列’ else print ’不是自增列’ SELECT * FROM sys.columns WHERE object_id=OBJECT_ID(’表名’) AND is_identity=1 if columnproperty(object_id(’table’),’col’,’IsIdentity’)=1 print ’自增列’else print ’不是自增列’SELECT * FROM sys.columns WHERE object_id=OBJECT_ID(’表名’)AND is_identity=1判斷表中是否存在索引Sql代碼if exists(select * from sysindexes where id=object_id(’表名’) and name=’索引名’) print ’存在’ else print ’不存在 if exists(select * from sysindexes where id=object_id(’表名’) and name=’索引名’) print ’存在’ else print ’不存在查看資料庫中對象Sql代碼SELECT * FROM sys.sysobjects WHERE name=’對象名’ SELECT * FROM sys.sysobjects WHERE name=’對象名’
摘抄自:http://www.111cn.net/database/mssqlserver/39107.htm
sql server 判斷是否存在資料庫,表,列,視圖