--1. get all indexes from current db, place in temp table
select schemaName = s.name, tablename = object_name(i.id), tableid = i.id, indexid = i.indid, indexname = i.name, i.status, isunique = indexproperty (i.id,i.name,'isunique'), isclustered = indexproperty (i.id,i.name,'isclustered'), indexfillfactor = indexproperty (i.id,i.name,'indexfillfactor')
into #tmp_indexes
from sysindexes i
INNER JOIN sys.tables t ON i.id = t.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
where i.indid > 0 and i.indid < 255 --not certain about this
and (i.status & 64) = 0 --existing indexes
--add additional columns to store include and key column lists
alter table #tmp_indexes add keycolumns varchar(4000), includes varchar(4000)
go
--################################################################################################
--2. loop through tables, put include and index columns into variables
declare @isql_key varchar(4000), @isql_incl varchar(4000), @tableid int, @indexid int
declare index_cursor cursor for
select tableid, indexid from #tmp_indexes
open index_cursor
fetch next from index_cursor into @tableid, @indexid
while @@fetch_status <> -1
begin select @isql_key = '', @isql_incl = '' select --i.name, sc.colid, sc.name, ic.index_id, ic.object_id, * --key column @isql_key = case ic.is_included_column when 0 then case ic.is_descending_key when 1 then @isql_key + coalesce(sc.name,'') + ' DESC, ' else @isql_key + coalesce(sc.name,'') + ' ASC, ' end else @isql_key end, --include column @isql_incl = case ic.is_included_column when 1 then case ic.is_descending_key when 1 then @isql_incl + coalesce(sc.name,'') + ', ' else @isql_incl + coalesce(sc.name,'') + ', ' end else @isql_incl end from sysindexes i INNER JOIN sys.index_columns AS ic ON (ic.column_id > 0 and (ic.key_ordinal > 0 or ic.partition_ordinal = 0 or ic.is_included_column != 0)) AND (ic.index_id=CAST(i.indid AS int) AND ic.object_id=i.id) INNER JOIN sys.columns AS sc ON sc.object_id = ic.object_id and sc.column_id = ic.column_id where i.indid > 0 and i.indid < 255 and (i.status & 64) = 0 and i.id = @tableid and i.indid = @indexid order by i.name, case ic.is_included_column when 1 then ic.index_column_id else ic.key_ordinal end if len(@isql_key) > 1 set @isql_key = left(@isql_key, len(@isql_key) -1) if len(@isql_incl) > 1 set @isql_incl = left(@isql_incl, len(@isql_incl) -1) update #tmp_indexes set keycolumns = @isql_key, includes = @isql_incl where tableid = @tableid and indexid = @indexid fetch next from index_cursor into @tableid,@indexid end
close index_cursor
deallocate index_cursor
--remove invalid indexes,ie ones without key columns
delete from #tmp_indexes where keycolumns = ''
--################################################################################################
--3. output the index creation scripts
set nocount on
--separator
select '---------------------------------------------------------------------'
--create index scripts (for backup)
SELECT 'CREATE ' + CASE WHEN ISUNIQUE = 1 THEN 'UNIQUE ' ELSE '' END + CASE WHEN ISCLUSTERED = 1 THEN 'CLUSTERED ' ELSE '' END + 'INDEX [' + INDEXNAME + ']' +' ON [' + schemaName + '].[' + TABLENAME + '] ' + '(' + keycolumns + ')' + CASE WHEN INDEXFILLFACTOR = 0 AND ISCLUSTERED = 1 AND INCLUDES = '' THEN '' WHEN INDEXFILLFACTOR = 0 AND ISCLUSTERED = 0 AND INCLUDES = '' THEN ' WITH (ONLINE = ON)' WHEN INDEXFILLFACTOR <> 0 AND ISCLUSTERED = 0 AND INCLUDES = '' THEN ' WITH (ONLINE = ON, FILLFACTOR = ' + CONVERT(VARCHAR(10),INDEXFILLFACTOR) + ')' WHEN INDEXFILLFACTOR = 0 AND ISCLUSTERED = 0 AND INCLUDES <> '' THEN ' INCLUDE (' + INCLUDES + ') WITH (ONLINE = ON)' ELSE ' INCLUDE(' + INCLUDES + ') WITH (FILLFACTOR = ' + CONVERT(VARCHAR(10),INDEXFILLFACTOR) + ', ONLINE = ON)' END
FROM #tmp_indexes
where left(tablename,3) not in ('sys', 'dt_') --exclude system tables
order by schemaName, tablename, indexid, indexname
set nocount off
drop table #tmp_indexes

参考:

http://www.sqlservercentral.com/Forums/Topic796512-391-1.aspx

脚本:SQLServer 2008 生成某数据库中的所有索引创建脚本的更多相关文章

  1. SQL Server 2008 R2 清空数据库中ldf日志文件

    /************************************************************ * Sql Server 2008 R2 清空数据库中ldf日志文件 * 将 ...

  2. Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...

  3. Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...

  4. C#读取Visual FoxPro(*.dbf)数据并使用SqlBulkCopy插入到SqlServer 2008 R2数据表中

    公司数据库从32位的SqlServer 2005升级到64位的SqlServer 2008 R2后,无法再像原来通过Link Server连接VFP同步数据,因此考虑用代码程序从VFP数据库中读取所需 ...

  5. SQLServer 命令批量删除数据库中指定表(游标循环删除)

    DECLARE @tablename VARCHAR(30),@sql VARCHAR(500)DECLARE cur_delete_table CURSOR READ_ONLY FORWARD_ON ...

  6. SQLSERVER如何获取一个数据库中的所有表的名称、一个表中所有字段的名称

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  7. [转]SQLSERVER如何获取一个数据库中的所有表的名称、一个表中所有字段的名称

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  8. Jmeter BeanShell 从数据库中获取数据并创建一个requesBody

    一.前言 在测试接口的时候常常会使用到数据库中的数据,当要使用大量的数据时,仅仅使用数据库查询是不够的.还需要使用自动化让操作更简便. 下面以一个简单的例子阐述一下如何使用beanShell让代码更简 ...

  9. mysql 快速生成删除数据库中所有的表的语句

    SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_s ...

随机推荐

  1. JQUERY 一些技巧在实际中的应用

    在jquery选择器中.继续做判断$("tr[rel!=" + id + "]").find('.status').html('--'); $("tr ...

  2. TVP5150摄像头

    工作中看同事摄像头配置的时候有2种格式PAL.NTSC.如果摄像头的格式配置不对的话会出现重影.黑白没颜色.闪屏等等. TVP5150  PAL.NTSC配置.http://bbs.csdn.net/ ...

  3. Memcache技术分享:介绍、使用、存储、算法、优化、命中率

    1.memcached 介绍 1.1 memcached 是什么? memcached 是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发 ...

  4. register instruction pointer

    Computer Science An Overview _J. Glenn Brookshear _11th Edition We have already encountered the conc ...

  5. 在Delphi中如何动态创建dbf数据库(二)?

    unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...

  6. php数据库两个关联大表的大数组分页处理,防止内存溢出

    $ret = self::$db->select($tables, $fields, $where, $bind); if (!empty($ret)) { $retIds = array(); ...

  7. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9

  8. Solaris 10下Qt编译Oracle 10g驱动

    上回书讲到<Oracle 10g在Solaris 10中安装详解>,现在开始用Qt来编译下Oracle 10g驱动吧!这样就可以通过Qt程序联入Oracle数据库了! Oracle的环境变 ...

  9. 【Java 基础篇】【第七课】组合

    我所理解的组合就是在一个类当中又包含了另一个类的对象. 这样的方式就是组合吧: 电池是一个类,有电量 手电筒需要电池 看代码吧: // 电池类 class Battery { // 充电 public ...

  10. AutoHotKey使用:空格键坏了怎么办?

    ;RCtrl:: RAlt:: Send, {space} Return http://ahkscript.org/ https://autohotkey.com/download/