zoukankan      html  css  js  c++  java
  • 删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005 .

    删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005,

    使用说明 :

    1,先执行脚本,将存储过程创建在数据库中

    2,调用方法,以黄金搭档数据库为例

    use velcromfm --数据库名, 根据具体项目替换

    go

    declare @tableName varchar(20)
    set @tableName='menu' --表名 ,根据实际情况替换
    exec sp_dropindex @tableName

    3,如有需要,该脚本稍加改写就可以做成一个删除数据库所有索引的脚本

    提供一个思路 :

    select * from sysobjects where xtype='u' --查除数据库里所有的表,使用游标遍历执行这个存储过程

    4,该脚本有个问题,如果某个pk索引是另外一个表的外键的话就删不掉,但在velcro系统的数据库不会有这个麻烦,应为我们的系统没有设任何外键^-^

    脚本如下

    */

    ----------------------------- 脚本---------------------------------------------------


    if exists(select 1 from sysobjects where id = object_id('sp_dropindex') and xtype = 'P')
    drop procedure sp_dropindex
    go

    create procedure sp_dropindex @tableName varchar(50)=null --表名
    as

    if @tableName is null
    begin
     raiserror('必须提供@tableName参数',12,1)
     return
    end

    create table # (
     id int identity,
     index_name varchar(50),
     index_description varchar(1000),
     index_keys varchar(100)
    )

    insert #(index_name,index_description,index_keys)
    exec sp_helpindex @tableName

    declare @i int
    declare @sql varchar(100)
     
    set @i = 1

    while @i<=(select max(id) from #)
    begin
     if exists(select 1 from sysobjects A join # B on A.name=B.index_name where B.id=@i and A.xtype in ('PK','UQ'))
     begin
      select @sql = 'alter table '+ @tableName +' drop constraint ' + (select index_name from # where id = @i)
     end
     else
     begin
      select @sql = 'drop index '+ @tableName + '.' + (select index_name from # where id=@i)
     end
     
    -- print(@sql)
        exec(@sql) 
     set @i=@i+1
    end

    drop table #

    go

  • 相关阅读:
    第四十一节 jQuery之bootstrap文档
    第四十节 jQuery之bootstrap简介
    Redis 如何实现查询附近的距离
    线上日志快速定位-grep
    Java字符串操作工具类
    JAVA批量插入数据操作+事务提交
    java开发需求中技术常见名称
    MySQL Binlog--MIXED模式下数据更新
    MySQL Replication--修改主键为NULL导致的异常
    MySQL Replication--复制异常1
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/3892634.html
Copyright © 2011-2022 走看看