if exists用法
阿新 • • 發佈:2019-02-01
自己留著,還把引號都轉了下=。=!換行等 格式化 呵呵 方便後面用
1 判斷資料庫是否存在
if exists (select * from sys.databases where name = '資料庫名')
drop database [資料庫名]
if exists (select * from sys.databases where name = '資料庫名')
drop database [資料庫名]
2 判斷表是否存在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 [表名]
3 判斷儲存過程是否存在if exists (select * from sysobjects where id = object_id(N'[儲存過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [儲存過程名]
4 判斷臨時表是否存在if exists (select * from sysobjects where id = object_id(N'[儲存過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [儲存過程名]
if object_id('tempdb..#臨時表名') is not null
drop table #臨時表名
if object_id('tempdb..#臨時表名') is not null
drop table #臨時表名
5 判斷檢視是否存在--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].[檢視名]'
6 判斷函式是否存在-- 判斷要建立的函式名是否存在
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].[函式名]
7 獲取使用者建立的物件資訊 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 = 擴充套件儲存過程
*/
8 判斷列是否存在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 列名
9 判斷列是否自增列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
10 判斷表中是否存在索引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 '不存在
11 檢視資料庫中物件SELECT * FROM sys.sysobjects WHERE name='物件名' SELECT * FROM sys.sysobjects WHERE name='物件名'