Files
sql-snippets/idx1-fb34ee85-4240-4e00-bc54-1388acccb026.json
2023-01-06 13:15:41 +01:00

6 lines
4.0 KiB
JSON

{
"id": "fb34ee85-4240-4e00-bc54-1388acccb026",
"prefix": "idx1",
"description": "list all indexes on a given table",
"body": ";WITH ColInfo AS \r\n(\tSELECT TblName = o.name\r\n\t\t\t,SchemaTbl = s.name + '.' + o.name\r\n\t\t\t,IndexName = i.name\r\n\t\t\t,IsPrimaryKey = i.is_primary_key\r\n\t\t\t,IsUniqueConstraint = i.is_unique_constraint\r\n\t\t\t,IsUnique = i.is_unique\r\n\t\t\t,ColName = c.name\r\n\t\t\t,IsComputedCol = c.is_computed\r\n\t\t\t,IsIncludedCol = ic.is_included_column\r\n\t\t\t,ic.key_ordinal\r\n\t\t\t,FilterDefinition = i.filter_definition\r\n\t\t\t,crlf = CHAR(13) + CHAR(10)\r\n\t\t\t,crlfgo = ';' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10) \r\n\tFROM sys.objects o \r\n\t\tINNER JOIN sys.schemas s ON o.schema_id = s.schema_id\r\n\t\tINNER JOIN sys.columns c ON o.object_id = c.object_id\r\n\t\tINNER JOIN sys.indexes i ON c.object_id = i.object_id\r\n\t\tINNER JOIN sys.index_columns ic ON i.index_id = ic.index_id AND o.object_id = ic.object_id AND c.column_id = ic.column_id\r\n\tWHERE o.name = '$CURSOR$'\r\n\t\t--AND i.filter_definition IS NOT NULL\r\n)\r\nSELECT DISTINCT \r\n\t\tx.TblName\r\n\t ,x.IndexName\r\n\t ,x.IsPrimaryKey\r\n\t ,x.IsUniqueConstraint\r\n\t ,x.IsUnique\r\n\t --,size.IndexSizeKB\r\n\t ,c.IndexColumns\r\n\t ,ci.IncludedColumns\r\n\t ,cc.ComputedColumns\r\n\t ,x.FilterDefinition\r\n\t ,DropCreateSQL = x.crlf + '-- ' + x.IndexName + x.crlf + \r\n\t\t\t\t\t\t--check drop\r\n\t\t\t\t\t\t'IF INDEXPROPERTY(OBJECT_ID('''+x.SchemaTbl+'''), '''+x.IndexName+''' , ''IndexID'' ) IS NOT NULL BEGIN;'+x.crlf+\r\n\t\t\t\t\t\tCASE WHEN x.IsPrimaryKey = 1 THEN NULL\r\n\t\t\t\t\t\t\tWHEN x.IsUniqueConstraint = 1 THEN \r\n\t\t\t\t\t\t\t--drop statement\r\n\t\t\t\t\t\t\t'\tALTER TABLE ' + x.SchemaTbl + ' DROP CONSTRAINT ' + x.IndexName + ';' + x.crlf \r\n\t\t\t\t\t\t\t+'END' + x.crlfgo\r\n\t\t\t\t\t\t\t--check create\r\n\t\t\t\t\t\t\t+'IF INDEXPROPERTY(OBJECT_ID('''+x.SchemaTbl+'''), '''+x.IndexName+''' , ''IndexID'' ) IS NULL BEGIN;'+x.crlf+\r\n\t\t\t\t\t\t\t--create statement\r\n\t\t\t\t\t\t\t+'\tALTER TABLE ' + x.SchemaTbl + ' ADD CONSTRAINT ' + x.IndexName + ' UNIQUE (' + c.IndexColumns + ');' + x.crlf \r\n\t\t\t\t\t\t\t+'END' + x.crlfgo\r\n\t\t\t\t\t\t\tELSE \r\n\t\t\t\t\t\t\t--drop statement\r\n\t\t\t\t\t\t\t'\tDROP INDEX ' + x.SchemaTbl + '.' + x.IndexName + ';'+ x.crlf\r\n\t\t\t\t\t\t\t+'END' + x.crlfgo\r\n\t\t\t\t\t\t\t--check create\r\n\t\t\t\t\t\t\t+'IF INDEXPROPERTY(OBJECT_ID('''+x.SchemaTbl+'''), '''+x.IndexName+''' , ''IndexID'' ) IS NULL BEGIN;'+x.crlf+\r\n\t\t\t\t\t\t\t--create statement\r\n\t\t\t\t\t\t\t+ '\tCREATE ' + CASE WHEN x.IsUnique = 1 THEN 'UNIQUE ' ELSE '' END + 'INDEX ' + x.IndexName + ' ON ' + x.SchemaTbl + '(' + c.IndexColumns + ')' \r\n\t\t\t\t\t\t\t+ ISNULL(' INCLUDE(' + ci.IncludedColumns + ')', '') \r\n\t\t\t\t\t\t\t+ ISNULL(' WHERE ' + x.FilterDefinition, '') + ';' + x.crlf \r\n\t\t\t\t\t\t\t+'END'+x.crlfgo\r\n\t\t\t\t\t\tEND\r\nFROM ColInfo x\r\nCROSS APPLY(SELECT\tIndexColumns = STUFF(sq.strXML, 1, 2, '')\r\n\t\t\tFROM (\tSELECT\t', ' + ISNULL(x2.ColName, '')\r\n\t\t\t\t\tFROM\tColInfo x2\r\n\t\t\t\t\tWHERE\tx.TblName = x2.TblName AND x.IndexName = x2.IndexName AND x2.IsIncludedCol = 0\r\n\t\t\t\t\tORDER\tBY x2.key_ordinal\r\n\t\t\t\t\tFOR XML PATH('')\r\n\t\t\t\t) sq (strXML)\r\n\t\t) c\r\nOuter APPLY(SELECT\tIncludedColumns = STUFF(sq.strXML, 1, 2, '')\r\n\t\t\tFROM (\tSELECT\t', ' + ISNULL(x2.ColName, '')\r\n\t\t\t\t\tFROM\tColInfo x2\r\n\t\t\t\t\tWHERE\tx.TblName = x2.TblName AND x.IndexName = x2.IndexName AND x2.IsIncludedCol = 1\r\n\t\t\t\t\tORDER\tBY x2.key_ordinal\r\n\t\t\t\t\tFOR XML PATH('')\r\n\t\t\t\t) sq (strXML)\r\n\t\t) ci\r\nOuter APPLY(SELECT\tComputedColumns = STUFF(sq.strXML, 1, 2, '')\r\n\t\t\tFROM (\tSELECT\t', ' + ISNULL(x2.ColName, '')\r\n\t\t\t\t\tFROM\tColInfo x2\r\n\t\t\t\t\tWHERE\tx.TblName = x2.TblName AND x.IndexName = x2.IndexName AND x2.IsComputedCol = 1\r\n\t\t\t\t\tORDER\tBY x2.key_ordinal\r\n\t\t\t\t\tFOR XML PATH('')\r\n\t\t\t\t) sq (strXML)\r\n\t\t) cc\r\n;\r\n"
}