ÈçºÎ¶ÔSQL ServerÊý¾Ý±íºÍÊý¾Ý¿â½øÐеü´ú²Ù×÷

ÈçºÎ¶ÔSQL ServerÊý¾Ý±íºÍÊý¾Ý¿â½øÐеü´ú²Ù×÷ - Ó¦ÓÃÈí¼þ - µçÄԽ̳ÌÍø

ÈçºÎ¶ÔSQL ServerÊý¾Ý±íºÍÊý¾Ý¿â½øÐеü´ú²Ù×÷

ÈÕÆÚ£º2007-03-08   ¼ö£º

±¾ÎĽéÉÜÁËmasterÊý¾Ý¿âÖÐÁ½¸ö·Ç³£ÓÐÓõ«ÔÚSQL ServerÔÚÏ߽̿ÆÊéÖÐûÓÐÌáµ½µÄ´æ´¢¹ý³Ì¡£

ÕâЩϵͳ¹ý³Ì¶ÔÓÚ´¦ÀíÒÔÏÂÈÎÎñ·Ç³£·½±ã£¬ÈçÅжÏʹÓõĴ洢¿Õ¼ä´óС¡¢ÐÐÊý¡¢Óû§±íË÷ÒýµÈµÈ¡£µÚÒ»¸ö¹ý³Ìsp_MSForEachDB¶ÔÓÚ¸ÐÐËȤµÄ·þÎñÆ÷ÉϵÄÿ¸öÊý¾Ý¿âÖ´ÐÐÈýÌõÃüÁî¡£

¡ô@command1£ºµÚÒ»¸öÖ´ÐеÄÃüÁî¡ô@replacechar£ºÓÃÁíÒ»¸öռλ¸³Ìæ»»¡°?¡±¡ô@command2£ºµÚ¶þ¸öÖ´ÐеÄÃüÁî¡ô@command3£ºµÚÈý¸öÖ´ÐеÄÃüÁî¡ô@precommand£º½øÈëÑ­»·Ç°Ö´ÐеÄÃüÁî¡ô@postcommand£ºÑ­»·½áÊøºóÖ´ÐеÄÃüÁî

ÿ¸öÃüÁ£¨¼´Ê¹¸Ã¼¯ºÏÖ»º¬ÓÐÒ»ÌõÃüÁ×÷Ϊһ¸öÅú´¦Àí¶Ôÿ¸öÊý¾Ý¿âÖ´ÐУ¬ËùÒÔµ±ÎÒÃÇÒª½«²¶»ñµÄ½á¹ûÊä³öµ½Îı¾¶ø²»ÊDZê×¼½á¹û¼¯±íʱ£¬Õ⽫·Ç³£ÓÐÓá£

ΪÁËʵÏÖÕâÒ»ÒªÇó£¬Ñ¡Ôñ²Ëµ¥ÖеIJéѯ°´Å¥|Êä³ö½á¹û|Êä³öµ½Îı¾»òÕß°´¿ì½Ý¼ü[Ctrl]T¡£

ÏÂÃæµÄ´úÂë·µ»Ø·þÎñÆ÷ÉÏÿ¸öÊý¾Ý¿âÖÐÓû§Êý¾Ý±íµÄÊýÄ¿£º

exec sp_MSForEachDB@command1 = "use ? exec sp_SpaceUsed"

The abbreviated output looks like this:¼ò¶ÌÊä³ö¿ÉÄÜÈçÏ£º

Êý¾Ý¿âÃûÊý¾Ý¿â´óСδ·ÖÅä¿Õ¼ä´óС

--------------------- ------------------ master 5.25 MB1.26 MB

reserved data index_size unused------------------ ------------------ 2808 KB 1144 KB 1080 KB 584 KB

µÚ¶þ¸ö¹ý³Ìsp_MSForEachTable½ÓÊÜ7¸ö²ÎÊý£º

¡ô@command1£ºµÚÒ»¸öÖ´ÐеÄÃüÁî¡ô@replacechar£ºÓÃÁíÒ»¸öռλ·ûÌæ»»¡°£¿¡±¡ô@command2£ºµÚ¶þ¸öÖ´ÐеÄÃüÁî¡ô@command3£ºµÚÈý¸öÖ´ÐеÄÃüÁî¡ô@whereand£ºWhereÌõ¼þÓï¾ä (»ò Order By Óï¾ä) ¡ô@precommand£º½øÈëÑ­»·Ç°Ö´ÐеÄÃüÁî ¡ô@postcommand£ºÑ­»·½áÊøºóÖ´ÐеÄÃüÁî

ͨ¹ý¶ÔÒª´«µÝµÄ²ÎÊýÃüÃû£¬¿ÉÒÔÌø¹ý´«µÝ¿ÕÖµµÄÒªÇó¡£µ±ÒªÖ´ÐеÄÃüÁîÖк¬ÓÐÒ»¸öÎʺÅʱ£¬²ÎÊý@replacecharÊ®·ÖÓÐÓá£@whereand²ÎÊýµÄʵÏÖ¿ÉÒÔ¸ù¾Ý¹ýÂËÆ÷ËõСÊä³öµÄ·¶Î§¡£

Ä㻹¿ÉÒÔ¼ÓÈëÒ»¸öORDER BYÓï¾ä¡£ÏÂÃæµÄÀý×Ó·µ»ØAdventureWorksÊý¾Ý¿âÖÐÿ¸öÊý¾Ý±íµÄÐÐÊý£¬²¢°´ÕÕÊý¾Ý±íÃ÷¶ÔËüÃÇÅÅÐò£º

exec sp_MSForEachTable@command1 = "Print '?'", @command2 = "select count(*) from ?", @whereand = "ORDER BY 1"

ÏÂÃæÊÇһЩÊä³ö½á¹û£º

[HumanResources].[Department]-----------16

[HumanResources].[Employee]-----------290

[HumanResources].[EmployeeAddress]-----------290

[HumanResources].[EmployeeDepartmentHistory]-----------296

ÎÒϲ»¶Í¨¹ýģʽºÍ±íÃû¶ÔÊý¾Ý±íÅÅÐò¡£

±êÇ©£º