
{"id":1212,"date":"2022-01-04T15:52:37","date_gmt":"2022-01-04T07:52:37","guid":{"rendered":"http:\/\/dba.qishuo.xin\/?p=1212"},"modified":"2022-01-04T15:52:37","modified_gmt":"2022-01-04T07:52:37","slug":"sql-server-%e6%89%b9%e9%87%8f%e5%af%bc%e5%87%ba%e7%b4%a2%e5%bc%95%e3%80%81%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e3%80%81%e8%a7%86%e5%9b%be%e5%92%8c%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/dba.qishuo.xin\/?p=1212","title":{"rendered":"Sql Server \u6279\u91cf\u5bfc\u51fa\u7d22\u5f15\u3001\u5b58\u50a8\u8fc7\u7a0b\u3001\u89c6\u56fe\u548c\u51fd\u6570"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<label for=\"ez-toc-cssicon-toggle-item-69fab69f18a51\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fab69f18a51\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/dba.qishuo.xin\/?p=1212\/#11_%E6%89%B9%E9%87%8F%E5%AF%BC%E5%87%BA%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E3%80%81%E5%87%BD%E6%95%B0%E5%92%8C%E8%A7%86%E5%9B%BE\" >1.1. \u6279\u91cf\u5bfc\u51fa\u5b58\u50a8\u8fc7\u7a0b\u3001\u51fd\u6570\u548c\u89c6\u56fe<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/dba.qishuo.xin\/?p=1212\/#12_%E6%89%B9%E9%87%8F%E5%AF%BC%E5%87%BA%E7%B4%A2%E5%BC%95\" >1.2. \u6279\u91cf\u5bfc\u51fa\u7d22\u5f15<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"11_%E6%89%B9%E9%87%8F%E5%AF%BC%E5%87%BA%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E3%80%81%E5%87%BD%E6%95%B0%E5%92%8C%E8%A7%86%E5%9B%BE\"><\/span>1.1. \u6279\u91cf\u5bfc\u51fa\u5b58\u50a8\u8fc7\u7a0b\u3001\u51fd\u6570\u548c\u89c6\u56fe<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-sql line-numbers\">SELECT a.name,a.[type],b.[definition]\n  FROM sys.all_objects a,sys.sql_modules b\n WHERE a.is_ms_shipped=0\n   AND a.object_id = b.object_id\n   AND a.[type] in ('P','V','AF')\n ORDER BY a.[name] asc;\n GO\n<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"12_%E6%89%B9%E9%87%8F%E5%AF%BC%E5%87%BA%E7%B4%A2%E5%BC%95\"><\/span>1.2. \u6279\u91cf\u5bfc\u51fa\u7d22\u5f15<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-sql line-numbers\">WITH indexInfo as (\n    SELECT SCHEMA_NAME(t.schema_id) [schema_name],t.name as [table_name],t1.name as [index_name]\n    ,t1.type,t1.type_desc,t1.is_unique,t1.is_primary_key,t1.is_unique_constraint,t1.has_filter,t1.filter_definition\n    ,STUFF((SELECT ','+t4.name FROM sys.sysindexkeys t2 \n        inner join sys.index_columns  t3 ON t2.id=t3.object_id and t2.indid=t3.index_id and t2.colid=t3.column_id\n        inner join sys.syscolumns t4 ON t2.id=t4.id and t2.colid=t4.colid\n        WHERE t2.id=t1.object_id and t1.index_id=t2.indid  and t2.keyno &lt;&gt; 0 ORDER BY t3.key_ordinal FOR XML PATH('')),1,1,'') AS index_cols\n    ,STUFF((SELECT ','+t4.name FROM sys.sysindexkeys t2 \n        inner join sys.index_columns  t3 ON t2.id=t3.object_id and t2.indid=t3.index_id and t2.colid=t3.column_id\n        inner join sys.syscolumns t4 ON t2.id=t4.id and t2.colid=t4.colid\n        WHERE t2.id=t1.object_id and t1.index_id=t2.indid  and t2.keyno = 0 ORDER BY t3.key_ordinal FOR XML PATH('')),1,1,'')  AS include_cols\n    FROM sys.tables as t\n    inner join sys.indexes as t1 on (t1.index_id &gt; 0 and t1.is_hypothetical = 0) and (t1.object_id=t.object_id)\n    WHERE t1.type in(1,2)\n), indexInfo2 AS (\nSELECT * ,(CASE \n    WHEN is_primary_key = 1 \n        THEN 'alter table '+[schema_name]+'.'+[table_name]+' add constraint '+[index_name]+' primary key '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+'('+index_cols+')'\n    WHEN is_unique = 1 AND is_unique_constraint = 1 \n        THEN 'alter table '+[schema_name]+'.'+[table_name]+' add constraint '+[index_name]+' unique '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+'('+index_cols+')'\n    WHEN is_unique = 1 AND (is_primary_key = 0 OR is_unique_constraint = 0)\n        THEN 'create unique '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+' index '+[index_name]+'  on '+[schema_name]+'.'+[table_name]+'('+index_cols+')'\n    ELSE 'create '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+' index '+[index_name]+' on '+[schema_name]+'.'+[table_name]+'('+index_cols+') '\n    END) script\nFROM indexInfo\n) SELECT [schema_name],[table_name],[index_name],script\n+(CASE WHEN include_cols IS NOT NULL THEN ' include('+include_cols+')' ELSE '' END)\n+(CASE WHEN has_filter = 1THEN ' where '+filter_definition ELSE '' END)\nFROM indexInfo2\nORDER BY [schema_name],[table_name],[type],[index_name],is_primary_key DESC,is_unique_constraint DESC,is_unique DESC;\nGO\n<\/code><\/pre>\n<p>\u53c2\u8003\uff1ahttps:\/\/www.cnblogs.com\/xunziji\/archive\/2012\/08\/22\/2650822.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.1. \u6279\u91cf\u5bfc\u51fa\u5b58\u50a8\u8fc7\u7a0b\u3001\u51fd\u6570\u548c\u89c6\u56fe SELEC&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[140,171,170,172],"class_list":["post-1212","post","type-post","status-publish","format-standard","hentry","category-sqlserver","tag-sqlserver","tag-171","tag-170","tag-172"],"_links":{"self":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/1212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1212"}],"version-history":[{"count":1,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1213,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/1212\/revisions\/1213"}],"wp:attachment":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}