1. 程式人生 > >SQL Server 如果表存在執行刪除

SQL Server 如果表存在執行刪除


use Budget  --使用Budget資料庫
go 

IF EXISTS(Select 1 From Sysobjects Where Name='costSeparateConfig')  --查詢表名costSeparateConfig是否存在
DROP table costSeparateConfig     --存在則刪除
GO

go
--建立新表
CREATE  TABLE  costSeparateConfig(      
	ID int IDENTITY(1,1) NOT NULL, 
	COMPANY_ID varchar(50) NOT NULL,
	IS_SEP varchar(50) NULL,
	CREATE_BY numeric(18, 0) NULL,
	CREATE_DATE datetime NULL,
	LAST_UPDATE_BY numeric(18, 0) NULL,
	LAST_UPDATE_DATE datetime NULL
)ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

IF EXISTS(Select Name From Sysobjects Where Name='P_ExistsDropView' And Xtype='P')
DROP PROC P_ExistsDropView
GO
Create Proc P_ExistsDropView
(
@DelView Varchar(200)
)
As

Declare @SQLText Varchar(1000)
If Exists(Select Top 1 Name From Sysobjects Where Name=@DelView And XType='V') 
Begin
  Set @SQLText=
'Drop View ' + @DelView Exec(@SQLText) End