{"id":79,"date":"2012-08-20T20:16:00","date_gmt":"2012-08-21T01:16:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=79"},"modified":"2023-07-26T12:38:22","modified_gmt":"2023-07-26T17:38:22","slug":"sql-server-check-if-index-exists-on-schema","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=79","title":{"rendered":"SQL Server : Check If Index Exists On Schema"},"content":{"rendered":"<div>\n<div>\n<span style=\"color: #38761d; font-family: &quot;Courier New&quot;, Courier, monospace;\"><strong>&#8211;create function:<\/strong><\/span><\/div>\n<p><\/p>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">SET<\/span> <span style=\"color: blue;\">ANSI_NULLS<\/span> <span style=\"color: blue;\">ON<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">GO<\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">SET<\/span> <span style=\"color: blue;\">QUOTED_IDENTIFIER<\/span> <span style=\"color: blue;\">ON<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">GO<\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">CREATE<\/span> <span style=\"color: blue;\">FUNCTION<\/span> [dbo]<span style=\"color: grey;\">.<\/span>[IndexExistsOnSchema]<u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"color: grey;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">(<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @SCHEMA <span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>50<span style=\"color: grey;\">),<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @INDEX&nbsp; <span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>255<span style=\"color: grey;\">)<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: grey;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">)<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">RETURNS<\/span> <span style=\"color: blue;\">INT<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">AS<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">BEGIN<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">DECLARE<\/span> @CNT <span style=\"color: blue;\">INT<\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">SET<\/span> @CNT <span style=\"color: grey;\">=<\/span> 0<\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">SELECT<\/span> @CNT <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">COUNT<\/span><span style=\"color: grey;\">(*)<\/span> <u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">FROM<\/span> <span style=\"color: green;\">SYS<\/span><span style=\"color: grey;\">.<\/span><span style=\"color: green;\">INDEXES<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">WHERE<\/span> <span style=\"color: magenta;\">OBJECT_ID<\/span> <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">OBJECT_ID<\/span><span style=\"color: grey;\">(<\/span>@SCHEMA<span style=\"color: grey;\">)<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: grey;\">AND<\/span> name <span style=\"color: grey;\">=<\/span> @INDEX<u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">RETURN<\/span> @CNT<u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">END<\/span><span style=\"color: blue;\">&nbsp;<\/span><\/span><\/div>\n<div>\n<span style=\"color: blue; font-family: &quot;Courier New&quot;, Courier, monospace;\">GO<\/span><\/div>\n<div>\n<span style=\"color: #38761d; font-family: &quot;Courier New&quot;, Courier, monospace;\"><strong>&#8211;use function:<\/strong><\/span><\/div>\n<p><span style=\"color: blue;\"><\/span><br \/>\n<span style=\"color: blue;\"><\/p>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">IF <\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">SELECT<\/span> DBO<span style=\"color: grey;\">.<\/span>INDEXEXISTSONSCHEMA<span style=\"color: grey;\">(<\/span>@SCHEMA_NAME<span style=\"color: grey;\">,<\/span> @INDEX_NAME<span style=\"color: grey;\">))<\/span> <span style=\"color: grey;\">&gt;<\/span> 0<u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">EXEC<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: red;\">&#8216;ALTER INDEX [INDEX_NAME] ON &#8216;<\/span> <span style=\"color: grey;\">+<\/span> @SCHEMA_NAME<span style=\"color: grey;\"><\/span> <span style=\"color: grey;\">+<\/span> <span style=\"color: red;\">&#8216; DISABLE&#8217;);<\/span><\/span><br \/><strong><span style=\"color: #38761d; font-family: Courier New;\">&#8211;or<\/span><\/strong><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">IF <\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">SELECT<\/span> DBO<span style=\"color: grey;\">.<\/span>INDEXEXISTSONSCHEMA<span style=\"color: grey;\">(<\/span>@SCHEMA_NAME<span style=\"color: grey;\"><\/span><span style=\"color: grey;\">,<\/span>@INDEX_NAME<span style=\"color: grey;\">))<\/span> <span style=\"color: grey;\">&gt;<\/span> 0<u><\/u><u><\/u><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\"><span style=\"color: blue;\">EXEC<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: red;\">&#8216;ALTER INDEX [INDEX_NAME] ON &#8216;<\/span> <span style=\"color: grey;\">+<\/span> @SCHEMA_NAME<span style=\"color: grey;\"><\/span><span style=\"color: grey;\">+<\/span> <span style=\"color: red;\">&#8216; REBUILD&#8217;<\/span><span style=\"color: grey;\">);<\/span><\/span><\/div>\n<p><\/span><\/p>\n<div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&#8211;create function: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[IndexExistsOnSchema] ( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @SCHEMA VARCHAR(50), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @INDEX&nbsp; VARCHAR(255) ) RETURNS INT AS BEGIN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DECLARE @CNT INT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET @CNT = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT @CNT = COUNT(*) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM SYS.INDEXES &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE OBJECT_ID = OBJECT_ID(@SCHEMA) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND name = @INDEX &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RETURN @CNT &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=79\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server : Check If Index Exists On Schema&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/79"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}