SQL SERVER 2005 獲取表的所有索引資訊以及刪除和新建語句
BEGIN
WITH tx AS
(
SELECT a.object_id
,b.name AS schema_name
,a.name AS table_name
,c.name as ix_name
,c.is_unique AS ix_unique
,c.type_desc AS ix_type_desc
,d.index_column_id
,d.is_included_column
,e.name AS column_name
,f.name AS fg_name
,d.is_descending_key AS is_descending_key
,c.is_primary_key
,c.is_unique_constraint
FROM sys.tables AS a
INNER JOIN sys.schemas AS b ON a.schema_id = b.schema_id AND a.is_ms_shipped = 0
INNER JOIN sys.indexes AS c ON a.object_id = c.object_id
INNER JOIN sys.index_columns AS d ON d.object_id = c.object_id AND d.index_id = c.index_id
INNER JOIN sys.columns AS e ON e.object_id = d.object_id AND e.column_id = d.column_id
INNER JOIN sys.data_spaces AS f ON f.data_space_id = c.data_space_id
)
SELECT
Drop_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' DROP CONSTRAINT ' + a.ix_name
ELSE 'DROP INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name + '.' + a.table_name END
,Create_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' ADD CONSTRAINT ' + a.ix_name
+ CASE WHEN a.is_primary_key = 1 THEN ' PRIMARY KEY' ELSE ' UNIQUE' END + '(' + indexColumns.ix_index_column_name + ')'
ELSE 'CREATE ' + CASE WHEN a.ix_unique = 1 THEN 'UNIQUE ' ELSE '' END
+ a.ix_type_desc + ' INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name
+ '.' + a.table_name + '(' + indexColumns.ix_index_column_name + ')'
+ CASE WHEN IncludeIndex.ix_included_column_name IS NOT NULL THEN ' INCLUDE (' + IncludeIndex.ix_included_column_name + ')' ELSE '' END
+ ' ON [' + a.fg_name +']' END
,CASE WHEN a.ix_unique = 1 THEN 'UNIQUE' END AS ix_unique
,a.ix_type_desc
,a.ix_name
,a.schema_name
,a.table_name
,indexColumns.ix_index_column_name
,IncludeIndex.ix_included_column_name
,a.fg_name
,a.is_primary_key
,a.is_unique_constraint
FROM
(
SELECT DISTINCT
ix_unique
,ix_type_desc
,ix_name
,schema_name
,table_name
,fg_name
,is_primary_key
,is_unique_constraint
FROM tx
) AS a
OUTER APPLY
(
SELECT ix_index_column_name
= STUFF((
SELECT ',' + column_name + CASE WHEN is_descending_key = 1 THEN ' DESC' ELSE '' END
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=0
ORDER BY index_column_id
FOR XML PATH('')
),1,1,'')
)IndexColumns
OUTER APPLY
(
SELECT ix_included_column_name
= STUFF((
SELECT ',' + column_name
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=1
ORDER BY index_column_id
FOR XML PATH('')
), 1,1,'')
)IncludeIndex
ORDER BY a.schema_name,a.table_name,a.ix_name;
END
WITH tx AS
(
SELECT a.object_id
,b.name AS schema_name
,a.name AS table_name
,c.name as ix_name
,c.is_unique AS ix_unique
,c.type_desc AS ix_type_desc
,d.index_column_id
,d.is_included_column
,e.name AS column_name
,f.name AS fg_name
,d.is_descending_key AS is_descending_key
,c.is_primary_key
,c.is_unique_constraint
FROM sys.tables AS a
INNER JOIN sys.schemas AS b ON a.schema_id = b.schema_id AND a.is_ms_shipped = 0
INNER JOIN sys.indexes AS c ON a.object_id = c.object_id
INNER JOIN sys.index_columns AS d ON d.object_id = c.object_id AND d.index_id = c.index_id
INNER JOIN sys.columns AS e ON e.object_id = d.object_id AND e.column_id = d.column_id
INNER JOIN sys.data_spaces AS f ON f.data_space_id = c.data_space_id
)
SELECT
Drop_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' DROP CONSTRAINT ' + a.ix_name
ELSE 'DROP INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name + '.' + a.table_name END
,Create_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' ADD CONSTRAINT ' + a.ix_name
+ CASE WHEN a.is_primary_key = 1 THEN ' PRIMARY KEY' ELSE ' UNIQUE' END + '(' + indexColumns.ix_index_column_name + ')'
ELSE 'CREATE ' + CASE WHEN a.ix_unique = 1 THEN 'UNIQUE ' ELSE '' END
+ a.ix_type_desc + ' INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name
+ '.' + a.table_name + '(' + indexColumns.ix_index_column_name + ')'
+ CASE WHEN IncludeIndex.ix_included_column_name IS NOT NULL THEN ' INCLUDE (' + IncludeIndex.ix_included_column_name + ')' ELSE '' END
+ ' ON [' + a.fg_name +']' END
,CASE WHEN a.ix_unique = 1 THEN 'UNIQUE' END AS ix_unique
,a.ix_type_desc
,a.ix_name
,a.schema_name
,a.table_name
,indexColumns.ix_index_column_name
,IncludeIndex.ix_included_column_name
,a.fg_name
,a.is_primary_key
,a.is_unique_constraint
FROM
(
SELECT DISTINCT
ix_unique
,ix_type_desc
,ix_name
,schema_name
,table_name
,fg_name
,is_primary_key
,is_unique_constraint
FROM tx
) AS a
OUTER APPLY
(
SELECT ix_index_column_name
= STUFF((
SELECT ',' + column_name + CASE WHEN is_descending_key = 1 THEN ' DESC' ELSE '' END
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=0
ORDER BY index_column_id
FOR XML PATH('')
),1,1,'')
)IndexColumns
OUTER APPLY
(
SELECT ix_included_column_name
= STUFF((
SELECT ',' + column_name
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=1
ORDER BY index_column_id
FOR XML PATH('')
), 1,1,'')
)IncludeIndex
ORDER BY a.schema_name,a.table_name,a.ix_name;
END
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-609341/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle獲取所有表名資訊和獲取指定表名欄位資訊Oracle
- MySQL建立索引、修改索引、刪除索引的命令語句MySql索引
- sql2005 獲取表欄位資訊和檢視欄位資訊SQL
- SQL SERVER 資料庫查詢表和欄位資訊語句SQLServer資料庫
- mysql 動態新建以及刪除分割槽表MySql
- SQL Server 2005 中的分割槽表和索引應用SQLServer索引
- 刪除資料庫中所有儲存過程和函式的sql語句資料庫儲存過程函式SQL
- SQL Server語句刪除帶有預設值的欄位SQLServer
- ElasticSearch 獲取es資訊以及索引操作Elasticsearch索引
- Java 獲取Word中的所有插入和刪除修訂Java
- SQL Server中獲取資料庫名、表名、欄位名和欄位註釋的SQL語句SQLServer資料庫
- 使用SQL語句獲取SQLite中的表定義SQLite
- Cookie新增、獲取以及刪除操作Cookie
- 獲取sql server 2008表欄位資訊SQLServer
- 【轉】通過sql語句獲取資料庫的基本資訊SQL資料庫
- SQL Server中獲取一個資料庫的所有表的列數SQLServer資料庫
- SQL Server 2005 日誌刪除和日誌檔案限制SQLServer
- 獲取表的所有列及其型別資訊型別
- Laravel 獲取執行的sql語句LaravelSQL
- sql server 2005下奇怪的Delete Top 語句(downmoon)SQLServerdelete
- 獲取sql server資料庫中所有庫、表、欄位名的方法SQLServer資料庫
- 刪除sql server資料庫中所有資料SQLServer資料庫
- SQL刪除資料庫裡所有表的外來鍵,同時刪除所有使用者表SQL資料庫
- 常見的SQL語句(建立、刪除、切換)SQL
- 【Oracle】刪除所有表Oracle
- SQL 獲取SQL Server中兩個日期之間的所有日期SQLServer
- mysql刪除主鍵索引,刪除索引語法MySql索引
- 已安裝 SQL Server 2005 Express 工具。若要繼續,請刪除 SQL Server 2005 Express 工具SQLServerExpress
- cookie的設定、獲取和刪除Cookie
- 用SQL語句增加刪除修改欄位SQL
- 觸發器中獲取SQL語句觸發器SQL
- SQL 獲取SQL Server中日期最近7天之間的所有日期SQLServer
- 分析索引快速獲取索引資訊索引
- SQL Server表分割槽刪除詳情DSCCSQLServer
- oracle之 獲取建表ddl語句Oracle
- mysql捕捉所有SQL語句MySql
- SQL Server 索引和表體系結構(聚集索引)SQLServer索引
- 將SQL Server中所有表的列資訊顯示出來SQLServer