--判断数据库是否存在
IF EXISTS (SELECT * FROM MASTER.sys.sysdatabases WHERE NAME = '库名')
PRINT 'exists '
else
PRINT 'not exists'
-- 判断要创建的表名是否存在
IF EXISTS (Select * From sysObjects Where Name ='表名' And Type In ('S','U'))
PRINT 'exists'
ELSE
PRINT 'not exists'
GO
--判断要创建临时表是否存在
If Object_Id( 'Tempdb.dbo.#Test') Is Not NULL--#Test 为临时表名
Begin
print '存在 '
End
Else
Begin
print '不存在 '
End
-- 判断要创建的存储过程名是否存在
IF EXISTS (Select * From sysObjects Where Name ='存储过程名' And Type In ('S','P'))
PRINT 'exists'
ELSE
PRINT 'not exists'
GO
--判断列名是否存在
IF EXISTS (SELECT O.NAME AS 表名,C.NAME AS 列名 FROM SYSOBJECTS O INNER JOIN SYSCOLUMNS C ON O.ID=C.ID WHERE O.NAME= '表名' AND C.NAME = '列名')
SELECT 'EXISTS'
ELSE
SELECT 'NOT EXISTS'
--判斷 表A 列C 是否存在
IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T1 INNER JOIN SYSCOLUMNS T2 ON T1.ID=T2.ID WHERE T1.NAME='A' AND T2.NAME='C')
--判断表名存在的一个函数
IF COL_LENGTH( '表名','列名') IS NULL
PRINT 'not exists'
ELSE
PRINT 'exists'
--新增修改视图SP使用先DROP, 后CREATE的方式. 如果带*的视图使用sp_refreshview刷新. 前面要加 exec
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rpt_2033300001_v]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[rpt_2033300001_v]
GO
CREATE VIEW dbo.rpt_2033300001_v
AS
*********
--判断存储过程是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
-- 删除存储过程
drop procedure [dbo].[存储过程名]
GO
Create Procedure [dbo].[存储过程名]
@参数名 VARCHAR(50) --...
AS