標籤:io ar 使用 for 資料 on cti as 資料庫
-- SQL SERVER 判斷是否存在某個觸發器、儲存過程
-- 判斷儲存過程,如果存在則刪除
IF (EXISTS(SELECT * FROM sysobjects WHERE name=‘procedurename‘ AND type=‘P‘))
DROP PROCEDURE procedurename
-- 判斷觸發器,如果存在則刪除
IF (EXISTS(SELECT * FROM sysobjects WHERE id=object_id(N‘[dbo].[triggername]‘) AND OBJECTPROPERTY(id, N‘IsTrigger‘) = 1))
DROP TRIGGER triggername
-- 判斷使用者函數是否存在,如果存在則刪除
-- 此處type有兩種: ‘TF‘- Table-Value Function 資料表值函式 ‘FN‘- Scalar-Value Function 純量值函式
IF (EXISTS(SELECT * FROM sysobjects WHERE id=object_id(N‘[dbo].[userfunction]‘) AND (type=‘FN‘ OR type=‘TF‘)))
DROP FUNCTION userfunction
-- 判斷視圖是否存在,存在則刪除
IF (EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME=N‘viewname‘))
DROP VIEW viewname
-- 判斷使用者表 是否存在,若存在則刪除
IF (EXISTS(SELECT * FROM sysobjects WHERE id=N‘tablename‘ AND OBJECTPROPERTY(id, N‘IsUserTable‘)=1))
DROP TABLE tablename
-- 判斷資料庫,如果存在則刪除
IF (EXISTS(SELECT * FROM master.dbo.sysdatabases WHERE dbid=db_ID(‘dbname‘)))
DROP DATABASE dbname
-- 如果提示:刪除資料庫時提示資料庫正在被使用,無法刪除(Cannot drop database databasename because it is currently in use),使用:
IF (EXISTS(SELECT * FROM master.dbo.sysdatabases WHERE dbid=db_ID(‘dbname‘)))
BEGIN
USE master
ALTER DATABASE dbname
SET single_user
WITH ROLLBACK IMMEDIATE
DROP DATABASE dbname
END
SQL SERVER 判斷是否存在並刪除某個資料庫、表、視圖、觸發器、儲存過程、函數