{"id":718,"date":"2010-04-13T10:40:00","date_gmt":"2010-04-13T10:40:00","guid":{"rendered":"\/blogs\/paul\/post\/A-DBA-myth-a-day-(1330)-you-cannot-run-DMVs-when-in-the-80-compat-mode-(T-SQL-Tuesday-005).aspx"},"modified":"2017-07-03T15:01:58","modified_gmt":"2017-07-03T22:01:58","slug":"a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/","title":{"rendered":"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005)"},"content":{"rendered":"<p style=\"text-align: justify;\">(Look in the <a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/category\/misconceptions\/\">Misconceptions<\/a> blog category for the rest of the month&#8217;s posts and check out the 60-page PDF with all the myths and misconceptions blog posts collected together when you join our <a href=\"https:\/\/www.sqlskills.com\/join-the-sqlskills-insider-community\/\" target=\"_blank\" rel=\"noopener noreferrer\">Insider list<\/a>, plus my online <a href=\"https:\/\/www.pluralsight.com\/courses\/sqlserver-myths\" target=\"_blank\" rel=\"noopener noreferrer\">Myths and Misconceptions training course<\/a> on Pluralsight.)<\/p>\n<p style=\"text-align: justify;\">This blog post is part of two series &#8211; my Myth-A-Day series and the monthly T-SQL Tuesday series that fellow-MVP Adam Machanic (<a href=\"https:\/\/twitter.com\/adammachanic\">twitter<\/a>|<a href=\"http:\/\/sqlblog.com\/blogs\/adam_machanic\/default.aspx\">blog<\/a>) organizes. This month&#8217;s T-SQL Tuesday is being run by Aaron Nelson (<a href=\"https:\/\/twitter.com\/SQLvariant\">twitter<\/a>|<a href=\"http:\/\/sqlvariant.com\/\">blog<\/a>) and is on the subject of reporting &#8211; see <a href=\"http:\/\/sqlvariant.com\/2010\/04\/t-sql-tuesday-005-reporting\/\">this blog post<\/a> for details.<\/p>\n<p style=\"text-align: justify;\"><strong>Myth #13:<\/strong> <em>you cannot run DMVs when in the 80 compat mode<\/em>.<\/p>\n<p><strong><u><em>FALSE <\/em><\/u><\/strong><\/p>\n<p style=\"text-align: justify;\">To start with, there&#8217;s a lot of confusion about what compat mode means. Does it mean that the database can be restored\/attached to a SQL Server 2000 server? No. It means that some T-SQL parsing, query plan behavior, hints and a few other things behave as they did in SQL Server 2000 (or 2005, if you&#8217;re setting it to 90 on a 2008 instance).<\/p>\n<p style=\"text-align: justify;\">In SQL Server 2008 you can use <em>ALTER DATABASE SET COMPATIBILITY_LEVEL<\/em> to change the compatibility level; in prior versions you use <em>sp_dbcmptlevel<\/em>. To see what the compatibility level controls, see the Books Online entry <em><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/alter-database-transact-sql-compatibility-level\" target=\"_blank\" rel=\"noopener noreferrer\">ALTER DATABASE Compatibility Level<\/a><\/em>.<\/p>\n<p style=\"text-align: justify;\">Compatibility level has no effect on the database physical version &#8211; which is what gets bumped up when you upgrade, and prevents a database being restored\/attached to a previous version &#8211; as they have a maximum physical version number they can understand. See my blog post <em><a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-13-difference-between-database-version-and-database-compatibility-level\/\">Search Engine Q&amp;A #13: Difference between database version and database compatibility level<\/a><\/em> for more details, and <em><a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/msg-602-level-21-state-50-line-1\/\">Msg 602, Level 21, State 50, Line 1<\/a><\/em> for details on the error messages you get when trying to attach\/restore a database to a previous version.<\/p>\n<p style=\"text-align: justify;\">But I digress, as usual :-)<\/p>\n<p style=\"text-align: justify;\">One of the things that looks like it doesn&#8217;t work is using DMVs when in the 80 compat mode. Here&#8217;s a simple script to show you, using SQL Server 2005:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCREATE DATABASE &#x5B;DMVTest];\r\nGO\r\nUSE &#x5B;DMVTest];\r\nGO\r\nCREATE TABLE &#x5B;t1] (&#x5B;c1] INT);\r\nCREATE CLUSTERED INDEX &#x5B;t1c1] on &#x5B;t1] (&#x5B;c1]);\r\nINSERT INTO &#x5B;t1] VALUES (1);\r\nGO\r\n\r\nEXEC sp_dbcmptlevel DMVTest, 80;\r\nGO\r\n\r\nSELECT * FROM sys.dm_db_index_physical_stats (\r\n    DB_ID (N'DMVTest'), -- database ID\r\n    OBJECT_ID (N't1'),  -- object ID\r\n    NULL,               -- index ID\r\n    NULL,               -- partition ID\r\n    'DETAILED');        -- scan mode\r\nGO\r\n<\/pre>\n<p>And the really useful error I get back is:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nMsg 102, Level 15, State 1, Line 2\r\nIncorrect syntax near '('.\r\n<\/pre>\n<p style=\"text-align: justify;\">How incredibly useful is that? It pinpoints the problem exactly &#8211; not.<\/p>\n<p style=\"text-align: justify;\">Edit: After writing this I realized I&#8217;d fallen victim to my own myth too! DMVs *are* supported in the 80 compat-mode completely. What&#8217;s *not* supported is calling a function (e.g. <em>OBJECT_ID<\/em>) as one of the DMV parameters. Thanks to Aaron Bertrand for point this out! (Apparently he pointed that out in the recent Boston class we taught, but I missed it.)<\/p>\n<p style=\"text-align: justify;\">Here&#8217;s the trick to using the functions as parameters. You change context\u00a0to a database in the 90 or higher compatibility level &#8211; and then you can point the DMV at\u00a0the database in the 80 compatibility level.<\/p>\n<p>Very cool. Check it out:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE &#x5B;master];\r\nGO&lt;\/span&gt;\r\n\r\nSELECT * FROM sys.dm_db_index_physical_stats (\r\n    DB_ID (N'DMVTest'),\r\n    OBJECT_ID (N'DMVTest..t1'),\r\n    NULL,\r\n    NULL,\r\n    'DETAILED');\r\nGO\r\n<\/pre>\n<p style=\"text-align: justify;\">And it works, even though the database\u00a0DMVTest is in the 80 compatibility level.<\/p>\n<p style=\"text-align: justify;\">One thing to be *very* careful of &#8211; you need to make sure you&#8217;re using the correct object ID. If I&#8217;d just left the second parameter as <em>OBJECT_ID (&#8216;t1&#8217;)<\/em>, it would have tried to find the object ID of the <em>t1<\/em> table in the <em>master<\/em> database. If it didn&#8217;t find it, it will use the value NULL, which will cause the DMV to run against all tables in the <em>DMVTest<\/em> database. If by chance there&#8217;s a <em>t1<\/em> table in <em>master<\/em>, it&#8217;s likely got a different object ID from the <em>t1<\/em> table in <em>DMVTest<\/em>, and so the DMV will fail.<\/p>\n<p style=\"text-align: justify;\">And <em>sys.dm_db_index_physical_stats<\/em> isn&#8217;t a true DMV &#8211; Dynamic Management View &#8211; it&#8217;s a Dynamic Management Function which does a *ton* of work potentially to return results &#8211; so you want to make sure you limit it to only the tables you&#8217;re interested in. See my recent blog post <em><a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-sys-dm_db_index_physical_stats\/\">Inside sys.dm_db_index_physical_stats<\/a><\/em> for details of how it works and how expensive it can be.<\/p>\n<p style=\"text-align: justify;\">So, you&#8217;ll need to use the new 3-part naming option\u00a0of <em>OBJECT_ID<\/em> in SQL Server 2005 onward to make sure you&#8217;re grabbing the correct object ID when going across database contexts.<\/p>\n<p style=\"text-align: justify;\">Another way to do it is to use variables and pre-assign the values to them, which you can do from within the 80 compat-mode database:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDECLARE @databaseID INT;\r\nDECLARE @objectID\u00a0 \u00a0INT;\r\n\r\nSELECT @databaseID = DB_ID (N'DMVTest');\r\nSELECT @objectID\u00a0\u00a0 = OBJECT_ID (N't1');\r\n\r\nSELECT * FROM sys.dm_db_index_physical_stats (\r\n    @dbid,\r\n    @objid,\r\n    NULL,\r\n    NULL,\r\n    'DETAILED');\r\nGO\r\n<\/pre>\n<p>Bottom line: another myth bites the dust!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Look in the Misconceptions blog category for the rest of the month&#8217;s posts and check out the 60-page PDF with all the myths and misconceptions blog posts collected together when you join our Insider list, plus my online Myths and Misconceptions training course on Pluralsight.) This blog post is part of two series &#8211; my [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,61,66,95],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-database-maintenance","category-misconceptions","category-performance-tuning","category-tools"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"(Look in the Misconceptions blog category for the rest of the month&#8217;s posts and check out the 60-page PDF with all the myths and misconceptions blog posts collected together when you join our Insider list, plus my online Myths and Misconceptions training course on Pluralsight.) This blog post is part of two series &#8211; my [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2010-04-13T10:40:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-03T22:01:58+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/\",\"name\":\"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2010-04-13T10:40:00+00:00\",\"dateModified\":\"2017-07-03T22:01:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/","og_locale":"en_US","og_type":"article","og_title":"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal","og_description":"(Look in the Misconceptions blog category for the rest of the month&#8217;s posts and check out the 60-page PDF with all the myths and misconceptions blog posts collected together when you join our Insider list, plus my online Myths and Misconceptions training course on Pluralsight.) This blog post is part of two series &#8211; my [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/","og_site_name":"Paul S. Randal","article_published_time":"2010-04-13T10:40:00+00:00","article_modified_time":"2017-07-03T22:01:58+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/","name":"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005) - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2010-04-13T10:40:00+00:00","dateModified":"2017-07-03T22:01:58+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/a-dba-myth-a-day-1330-you-cannot-run-dmvs-when-in-the-80-compat-mode-t-sql-tuesday-005\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"A DBA myth a day: (13\/30) you cannot run DMVs when in the 80 compat mode (T-SQL Tuesday #005)"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}