{"id":407,"date":"2011-02-07T22:07:29","date_gmt":"2011-02-08T00:07:29","guid":{"rendered":"http:\/\/fabriciolima.net\/blog\/?p=407"},"modified":"2016-07-23T22:47:59","modified_gmt":"2016-07-24T01:47:59","slug":"querys-do-dia-a-dia-como-visualizar-as-colunas-include-de-um-indice","status":"publish","type":"post","link":"https:\/\/fabriciolima.net\/blog\/2011\/02\/07\/querys-do-dia-a-dia-como-visualizar-as-colunas-include-de-um-indice\/","title":{"rendered":"Querys do Dia a Dia: Como visualizar as colunas INCLUDE de um \u00cdndice"},"content":{"rendered":"<p>Fala Pessoal,<\/p>\n<p>Diariamente preciso\u00a0visualizar todos os \u00edndices de uma tabela para analisar a cria\u00e7\u00e3o, altera\u00e7\u00e3o ou exclus\u00e3o de um \u00edndice.<\/p>\n<p>A forma mais r\u00e1pida de ver os \u00edndices de uma tabela espec\u00edfica \u00e9 selecionar o nome da tabela e apertar Alt+f1. Entretanto, essa maneira n\u00e3o nos mostra as colunas da cl\u00e1usula include de um \u00edndice. Para obter essa informa\u00e7\u00e3o, utilizo a query abaixo:<\/p>\n<p>DECLARE @Nm_Tabela varchar(30)<br \/>\nSET @Nm_Tabela = &#8216;Nome_Tabela&#8217;<\/p>\n<p>SELECT SCHEMA_NAME (o.SCHEMA_ID) SchemaName<br \/>\n,o.name ObjectName,i.name IndexName<br \/>\n,i.type_desc<br \/>\n,LEFT(list, ISNULL(splitter-1,len(list))) Columns<br \/>\n, SUBSTRING(list, indCol.splitter +1, 1000) includedColumns&#8211;len(name) &#8211; splitter-1) columns<br \/>\n, COUNT(1) over (partition by o.object_id)<br \/>\nFROM sys.indexes i<br \/>\njoin sys.objects o on i.object_id = o.object_id<br \/>\ncross apply (select NULLIF(charindex(&#8216;|&#8217;,indexCols.list),0) splitter , list<br \/>\nfrom (select cast((<br \/>\nselect case when sc.is_included_column = 1 and sc.ColPos = 1 then&#8217;|&#8217; else &#8221; end +<br \/>\ncase when sc.ColPos\u00a0 &gt; 1 then &#8216;, &#8216; else &#8221; end + name<br \/>\nfrom (select sc.is_included_column, index_column_id, name<br \/>\n, ROW_NUMBER() over (partition by sc.is_included_column<br \/>\norder by sc.index_column_id)ColPos<br \/>\nfrom sys.index_columns\u00a0 sc<br \/>\njoin sys.columns\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c on sc.object_id = c.object_id<br \/>\nand sc.column_id = c.column_id<br \/>\nwhere sc.index_id = i.index_id<br \/>\nand sc.object_id = i.object_id ) sc<br \/>\norder by sc.is_included_column<br \/>\n,ColPos<br \/>\nfor xml path (&#8221;), type) as varchar(max)) list)indexCols ) indCol<br \/>\nWHERE indCol.splitter is not null<br \/>\nand o.name = @Nm_Tabela<br \/>\nORDER BY SchemaName, ObjectName, IndexName<\/p>\n<p>Entretanto, essa query retorna apenas os \u00edndices que possuem a cl\u00e1usula INCLUDE.\u00a0Para ver todos os \u00edndices com ou sem a cl\u00e1usula INCLUDE\u00a0podemos utilizar uma procedure cujo c\u00f3digo foi disponibilizado por Kimberly L. Tripp (<a onclick=\"javascript:_gaq.push(['_trackEvent','outbound-article','www.sqlskills.com']);\" href=\"http:\/\/www.sqlskills.com\/BLOGS\/KIMBERLY\/\" target=\"_blank\">Blog<\/a>|<a onclick=\"javascript:_gaq.push(['_trackEvent','outbound-article','twitter.com']);\" href=\"http:\/\/twitter.com\/KimberlyLTripp\" target=\"_blank\">Twitter<\/a>): <a title=\"Sp_helpindex2\" href=\"http:\/\/www.sqlskills.com\/blogs\/kimberly\/post\/Updates-%28fixes%29-to-sp_helpindex2.aspx\" target=\"_blank\">sp_helpindex_2<\/a>.\u00a0Depois de criar a procedure, basta execut\u00e1-la conforme a figura abaixo:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-408 aligncenter\" title=\"sp_helpindex2\" src=\"https:\/\/www.fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-300x84.png\" alt=\"Procedure sp_helpindex2\" width=\"300\" height=\"84\" srcset=\"https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-300x84.png 300w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-410x115.png 410w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-100x28.png 100w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-275x77.png 275w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2-20x6.png 20w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2011\/02\/sp_helpindex2.png 650w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><b>Gostou dessa dica?<\/b><\/p>\n<p>Cadastre seu e-mail para receber novos Posts e curta minha <a href=\"https:\/\/www.facebook.com\/FabricioLimaSolucoesemBancodeDados\/\" target=\"_blank\">P\u00e1gina no Facebook<\/a> para receber Dicas de Leituras e Eventos sobre SQL Server.<\/p>\n<p>Aprenda a criar essa e outras rotinas para administrar melhor seu SQL Server no meu <a href=\"https:\/\/www.fabriciolima.net\/cursos-online\/treinamento-tarefas-do-dia-a-dia-de-um-dba-online\/\" target=\"_blank\">Treinamento de Tarefas do Dia a Dia de um DBA<\/a>.<\/p>\n<p>Abra\u00e7os,<\/p>\n<p>Fabr\u00edcio Lima<\/p>\n<p>MCITP \u2013 Database Administrator<\/p>\n<p>Consultor e Instrutor SQL Server<\/p>\n<p>Trabalha com SQL Server desde 2006<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fala Pessoal, Diariamente preciso\u00a0visualizar todos os \u00edndices de uma tabela para analisar a cria\u00e7\u00e3o, altera\u00e7\u00e3o ou exclus\u00e3o de um \u00edndice. A forma mais r\u00e1pida de ver os \u00edndices de uma tabela espec\u00edfica \u00e9 selecionar o nome da tabela e apertar Alt+f1. Entretanto, essa maneira n\u00e3o nos mostra as colunas da cl\u00e1usula include de um \u00edndice. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[13,280],"tags":[176,23,175,110,179,63,49,33,40],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-querys-do-dia-a-dia","category-virtual-pass-br","tag-clausula-include","tag-dba","tag-include","tag-indice","tag-manutencao-de-indice","tag-performance","tag-sql","tag-sql-server","tag-tuning"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":0,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"wp:attachment":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}