博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

--判断数据库是否存在 
IF EXISTS (SELECT * FROM MASTER..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'

判断表名存在的一个函数

 

 IF COL_LENGTH( '表名','列名') IS NULL
    PRINT 'not exists'
ELSE
 PRINT 'exists'

 

注:a 是一个表,U代表是数据表类型

类似于U的类型代码,如下所示

对象类型:

AF = 聚合函数 (CLR)

C = CHECK 约束

D = DEFAULT(约束或独立)

F = FOREIGN KEY 约束

PK = PRIMARY KEY 约束

P = SQL 存储过程

PC = 程序集 (CLR) 存储过程

FN = SQL 标量函数

FS = 程序集 (CLR) 标量函数

FT = 程序集 (CLR) 表值函数

R = 规则(旧式,独立)

RF = 复制筛选过程

SN = 同义词

SQ = <strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u670D_u52A1";KeyGate_ads.ShowGgAds(this,"_u670D_u52A1",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://www.google.com/aclk?sa=L&ai=Bh2rlOAWHR56sBozu6AOroqCAC5_q_jCXvsOWA_iU2JQFwKkHEAIYAiCvmqoJKAM4AVDrnYft_f____8BYJ3h24HQBaoBCjEwMDAwMTYwMDLIAQHIAo-anALZAwUzhILJ-9To&num=2&q=http://www2.tek.com/cnweb/products/oscilloscopes/%3FWT.srch%3D1%26WT.mc_id%3Dppc,ggl,scope_aw_ch_cn_scope,k171E,s,766361139%26&sig=AGiWqtxyWRPsXCeA4J63cbgSQs3krP7s8Q");GgKwClickStat("服务","www.tektronix.com.cn","afs","1000016002");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u670D_u52A1"">服务队列

TA = 程序集 (CLR) DML <strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u89E6_u53D1_u5668";KeyGate_ads.ShowGgAds(this,"_u89E6_u53D1_u5668",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://www.google.com/aclk?sa=l&ai=BQIoCOAWHR56sBozu6AOroqCAC5iBlTOIz87RBPDWtnnAmgwQARgBIK-aqgkoAzABOAFQ7tXJy_7_____AWCd4duB0AWgAaqp9v4DqgEKMTAwMDAxNjAwMsgBAcgC6MOOAdkDBTOEgsn71OjgAxA&num=1&q=http://toolbar.google.com/T4/intl/zh-CN/%3Futm_campaign%3DzhCN%26utm_source%3DzhCN-ha-ww-google%26utm_medium%3Dha%26utm_term%3D%25E5%25B7%25A5%25E5%2585%25B7%26tbbrand%3DGZAZ&sig=AGiWqtzKjiNgl3U7aRUdC4tjyZq0F7XBBQ");GgKwClickStat("触发器","toolbar.google.com/zh-CN","afs","1000016002");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u89E6_u53D1_u5668"">触发器

TR = SQL DML 触发器

IF = SQL 内联表值函数

TF = SQL 表值函数

U = 表(用户定义类型)

UQ = UNIQUE 约束

V = 视图

X = 扩展存储过程

IT = 内部表