zoukankan      html  css  js  c++  java
  • 检查sql对象是否存在

    SQL Server判断对象是否存在   
    1 判断数据库是否存在 Sql代码  
    if exists (select * from sys.databases where name = '数据库名')     drop database [数据库名]   
    2 判断表是否存在 Sql代码  
    if exists (select * from sysobjects where id = object_id('表名') and objectproperty(id,'IsUserTable') = 1) drop table [表名]  
    if exists (select * from sysobjects where name = '表名' and xtype = 'U') drop table [表名]   
    3 判断存储过程是否存在 Sql代码  
    if exists (select * from sysobjects where id = object_id('[存储过程名]') and objectproperty(id, 'IsProcedure') = 1) drop procedure [存储过程名]   
    if exists (select 1 from sysobjects where name = '存储过程名' and xtype = 'P')  drop procedure [存储过程名]  
    4 判断临时表是否存在 Sql代码  
    if object_id('tempdb..#临时表名') is not null       drop table #临时表名   
    5 判断视图是否存在 Sql代码  
    --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 判断函数是否存在 Sql代码  
    --  判断要创建的函数名是否存在   
      if exists (select * from dbo.sysobjects where id = object_id('[dbo].[函数名]') and xtype in ('FN', 'IF', 'TF'))   


    wk_ad_begin({pid : 21});wk_ad_after(21, function(){$('.ad-hidden').hide();}, function(){$('.ad-hidden').show();});


      drop function [dbo].[函数名]  
      if exists (select 1 from sysobjects where name = '函数名' and xtype in('FN','IF','TF'))   drop function [函数名]    
    7 获取用户创建的对象信息  Sql代码  
    SELECT 1 FROM sysobjects where name = [对象名] and xtype=''   /*   
    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 判断列是否存在 Sql代码  
    if exists(select * from syscolumns where id=object_id('表名') and name='列名')     alter table 表名 drop column 列名     
    9 判断列是否自增列 Sql代码  
    if columnproperty(object_id('table'),'col','IsIdentity')=1     print '自增列'   else   
      print '不是自增列'    
    SELECT * FROM syscolumns WHERE object_id=OBJECT_ID('表名')   AND is_identity=1 


      
    10 判断表中是否存在索引 Sql代码  
    if exists(select * from sysindexes where id=object_id('表名') and name='索引名')       print  '存在'     else     
      print  '不存在'    
    11 查看数据库中对象 Sql代码  
    SELECT * FROM sys.sysobjects WHERE name='对象名'  
    12 查看表中主键是否存在,如存在则将其drop掉 declare @pk varchar(100) 
    select @pk=name from sysobjects where parent_obj=object_id('表名') and xtype='PK' if @pk is not null 
    exec('alter table 表名 drop '+ @pk)

  • 相关阅读:
    读jQuery之十六(事件代理)
    双向列表(JS)
    单向链表(JS)
    子程序(过程、函数、方法)
    jQuery(1.6.3) 中css方法对浮动的实现缺陷
    操作class属性的新APIclassList
    ajax后退解决方案(四)
    设置元素浮动的几种方式
    各浏览器中使用getAttribute获取checkbox/radio的checked值不同
    IE6/7不支持hashchange事件
  • 原文地址:https://www.cnblogs.com/guanshan/p/guan126.html
Copyright © 2011-2022 走看看