{"id":1817,"date":"2011-07-09T04:03:04","date_gmt":"2011-07-09T04:03:04","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1817"},"modified":"2014-07-31T03:01:30","modified_gmt":"2014-07-31T03:01:30","slug":"%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e8%a1%a8%e3%80%81%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2011\/07\/09\/%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e8%a1%a8%e3%80%81%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae%e5%ba%93\/","title":{"rendered":"\u904d\u5386\u6240\u6709\u8868\u3001\u904d\u5386\u6240\u6709\u6570\u636e\u5e93"},"content":{"rendered":"<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n\u7cfb\u7edf\u5b58\u50a8\u8fc7\u7a0bsp_MSforeachtable\u548csp_MSforeachdb,\u662f\u5fae\u8f6f\u63d0\u4f9b\u7684\u4e24\u4e2a\u4e0d\u516c\u5f00\u7684\u5b58\u50a8\u8fc7\u7a0b,\u4ecemssql6.5\u5f00\u59cb\u3002\u5b58\u653e\u5728SQL Server\u7684MASTER\u6570\u636e\u5e93\u4e2d\u3002\u53ef\u4ee5\u7528\u6765\u5bf9\u67d0\u4e2a\u6570\u636e\u5e93\u7684\u6240\u6709\u8868\u6216\u67d0\u4e2aSQL\u670d\u52a1\u5668\u4e0a\u7684\u6240\u6709\u6570\u636e\u5e93\u8fdb\u884c\u7ba1\u7406\r\n\u53c2\u6570\u8bf4\u660e:\r\n\r\n   @command1 nvarchar\uff082000\uff09,                      --\u7b2c\u4e00\u6761\u8fd0\u884c\u7684SQL\u6307\u4ee4\r\n   @replacechar nchar\uff081\uff09 = N&#39;?&#39;,                      --\u6307\u5b9a\u7684\u5360\u4f4d\u7b26\u53f7\uff0c\u9ed8\u8ba4\u4e3a?\r\n   @command2 nvarchar\uff082000\uff09= null,            --\u7b2c\u4e8c\u6761\u8fd0\u884c\u7684SQL\u6307\u4ee4\r\n   @command3 nvarchar\uff082000\uff09= null,            --\u7b2c\u4e09\u6761\u8fd0\u884c\u7684SQL\u6307\u4ee4\r\n   @whereand nvarchar\uff082000\uff09= null,               --\u53ef\u9009\u6761\u4ef6\u6765\u9009\u62e9\u8868\r\n   @precommand nvarchar\uff082000\uff09= null,        --\u6267\u884c\u6307\u4ee4\u524d\u7684\u64cd\u4f5c(\u7c7b\u4f3c\u63a7\u4ef6\u7684\u89e6\u53d1\u524d\u7684\u64cd\u4f5c)\r\n   @postcommand nvarchar\uff082000\uff09= null       --\u6267\u884c\u6307\u4ee4\u540e\u7684\u64cd\u4f5c(\u7c7b\u4f3c\u63a7\u4ef6\u7684\u89e6\u53d1\u540e\u7684\u64cd\u4f5c)\r\n\r\n   \u4ee5\u540e\u4e3asp_MSforeachtable\u7684\u53c2\u6570\uff0csp_MSforeachdb\u4e0d\u5305\u62ec\u53c2\u6570@whereand\r\n\r\n\u5982\uff1a\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u6240\u6709\u4ee5bd\u5f00\u5934\u7684\u8868\u7684\u5bb9\u91cf\r\n\r\n\u67e5\u8be2\u4e00\u6761\u7528:sp_spaceused\r\n\r\n\u591a\u6761\u7528\uff1a\r\nCREATE TABLE #TableSpaceUsed(\r\nTableName SYSNAME,\r\nRows INT,\r\nReserved VARCHAR(20),\r\nDataSize VARCHAR(20),\r\nIndexSize VARCHAR(20),\r\nUnUsed VARCHAR(20))\r\n\r\nEXEC sp_MSForEachTable\r\n@command1=N&#39;insert into #TableSpaceUsed exec sp_spaceused &#39;&#39;?&#39;&#39;&#39;,@whereand=&#39;and o.name like &#39;&#39;%&#39;&#39;&#39;\r\n\r\nSELECT * FROM #TableSpaceUsed\r\nORDER BY CONVERT(INT,REPLACE(Reserved,&#39; KB&#39;,&#39;&#39;)) DESC\r\n--\u66f4\u65b0PUBS\u6570\u636e\u5e93\u4e2d\u5df2t\u5f00\u5934\u7684\u6240\u6709\u8868\u7684\u7edf\u8ba1: EXEC sp_MSforeachtable @whereand=&quot;and name like &#39;t%&#39;&quot;, @replacechar=&#39;*&#39;, @precommand=&quot;print &#39;Updating Statistics.....&#39; print &#39;&#39;&quot;, @command1=&quot;print &#39;*&#39; update statistics * &quot;, @postcommand= &quot;print&#39;&#39;print &#39;Complete Update Statistics!&#39;&quot; --\u904d\u5386\u6570\u636e\u5e93\u6587\u4ef6\r\n\r\n--\u904d\u5386\u6570\u636e\u5e93\u6587\u4ef6\r\nCREATE TABLE #DataBaseFiles(\r\nDB SYSNAME,\r\nLogicName SYSNAME,\r\nphysical_name nvarchar(260),\r\nFILENAME nvarchar(260),\r\nFileSize DECIMAL(18,1)\r\n)\r\n\r\n--\u8981\u7528user ? \u5426\u5219\u8fd4\u56de\u7684\u7ed3\u679c\u4ecd\u662f\u5f53\u524d\u5e93\u7684\u6570\u636e\r\nEXEC sp_MSforeachdb &#39;USE ?  insert into #DataBaseFiles SELECT &#39;&#39;?&#39;&#39;,name, substring(filename,len(filename) - charindex(&#39;&#39;\\&#39;&#39;, reverse(filename))+2,charindex(&#39;&#39;\\&#39;&#39;, reverse(filename))),filename, CONVERT(DECIMAL(18,1),(size*8.0\/(1024*1024))) AS Size_GB FROM sys.sysfiles &#39;\r\n\r\nSELECT *\r\nFROM #DataBaseFiles\r\n\r\nRun same command on all SQL Server databases without cursors\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7cfb\u7edf\u5b58\u50a8\u8fc7\u7a0bsp_MSforeachtable\u548csp_MSforeachdb,\u662f\u5fae\u8f6f\u63d0\u4f9b\u7684\u4e24\u4e2a\u4e0d\u516c\u5f00\u7684\u5b58\u50a8\u8fc7\u7a0b [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1817","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1817"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1817\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}