{"id":7190,"date":"2022-04-11T07:06:00","date_gmt":"2022-04-11T05:06:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=7190"},"modified":"2022-11-28T17:01:44","modified_gmt":"2022-11-28T16:01:44","slug":"db-do-not-exists-error-sp-msforeachdb","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/","title":{"rendered":"Database does not exist error in sp_MSforeachdb"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\"><strong><em>How to fix the database does not exist error in sp_MSforeachdb ?<\/em><\/strong><\/h4>\n\n\n\n<p>With MS SQL Server, when using the sp_MSforeachdb system stored procedure, the system displays this error : Database does not exist. Make sure that the name is entered correctly. But only in specific cases and for certain databases.<\/p>\n\n\n\n<p>The stored procedure displays the error because the database tries to access a database that does not exist in your SQL Server instance. So it&#8217;s obviously not visible in the database list using SSMS. But somehow it appears in the list with the query execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-sql-error-database-does-not-exist-with-exec-sp-msforeachdb\">SQL Error database does not exist with EXEC sp_MSforeachdb<\/h2>\n\n\n\n<p>The full error message is displayed with the use of EXEC sp_msforeachdb, the details are as follows. There is actually <strong><em>no error<\/em><\/strong> in the <a href=\"https:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/16976.aspx\" target=\"_blank\" rel=\"noreferrer noopener\">sp_MSforeachdb\u00a0<\/a>system\u00a0stored\u00a0procedure\u00a0nor\u00a0in\u00a0the <em>sys.sp_MSforeach_worker<\/em>. This issue is a classical issue with the naming of the database.<\/p>\n\n\n\n<p><em>The database does not exist. Make sure the name is entered correctly.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-rename-the-databases-without-special-characters\">Rename the databases without special characters<\/h2>\n\n\n\n<p>If the SQL Server database names contain <strong>spaces or dashes<\/strong>, you can simply rename them or replace the characters with an <strong>underscore<\/strong>. Here is a T-SQL script example to rename a SQL Server database. To be adapted to your specific scenario: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">USE [master]\nGO\n\nALTER DATABASE [database name is-not-good] MODIFY NAME = Database_name_is_good_now;\nGO\n<\/pre>\n\n\n\n<p>Finally, do not use dashes and spaces in database names. It is technically possible to use them, but to use the system&#8217;s stored procedures, it is better to use only underscore characters. Or better still, don&#8217;t use any special characters in the names of your database objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using undocumented Microsoft system procedures<\/h2>\n\n\n\n<p>Another way to avoid the MS SQL Database does not exist error in sp_MSforeachdb, is to follow the best practice. Note that <strong>Microsoft strongly recommends not using these undocumented procedures in a Production environment<\/strong>. Indeed sp_msforeachdb is an undocumented system stored procedure and therefore not officially supported. The alternative solution is to use a loop on the name in a stored procedure.<\/p>\n\n\n\n<p>Finally, here is another article on <a href=\"https:\/\/expert-only.com\/en\/errors\/invalid-object-name-sql-server-cte-error\/\">how to handle the Invalid Object Name error with CTE SQL Server<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"tbZcrZX5wf\"><a href=\"https:\/\/expert-only.com\/en\/errors\/invalid-object-name-sql-server-cte-error\/\">Invalid object name SQL Server CTE Error<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Invalid object name SQL Server CTE Error&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/errors\/invalid-object-name-sql-server-cte-error\/embed\/#?secret=kXPpZ5q3cS#?secret=tbZcrZX5wf\" data-secret=\"tbZcrZX5wf\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to fix the database does not exist error in sp_MSforeachdb ? With MS SQL Server, when using the sp_MSforeachdb system stored procedure, the system displays this error : Database does not exist. Make sure that the name is <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\" title=\"Database does not exist error in sp_MSforeachdb\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5543,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[486],"tags":[],"class_list":{"0":"post-7190","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-errors"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Database does not exist error in sp_MSforeachdb - T-SQL error<\/title>\n<meta name=\"description\" content=\"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Database does not exist error in sp_MSforeachdb\" \/>\n<meta property=\"og:description\" content=\"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-11T05:06:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-28T16:01:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Database does not exist error in sp_MSforeachdb\",\"datePublished\":\"2022-04-11T05:06:00+00:00\",\"dateModified\":\"2022-11-28T16:01:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\"},\"wordCount\":364,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg\",\"articleSection\":[\"SQL Server errors\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\",\"url\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\",\"name\":\"Database does not exist error in sp_MSforeachdb - T-SQL error\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg\",\"datePublished\":\"2022-04-11T05:06:00+00:00\",\"dateModified\":\"2022-11-28T16:01:44+00:00\",\"description\":\"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Database does not exist error in sp_MSforeachdb\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Database does not exist error in sp_MSforeachdb - T-SQL error","description":"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.","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:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/","og_locale":"en_US","og_type":"article","og_title":"Database does not exist error in sp_MSforeachdb","og_description":"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.","og_url":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-04-11T05:06:00+00:00","article_modified_time":"2022-11-28T16:01:44+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Database does not exist error in sp_MSforeachdb","datePublished":"2022-04-11T05:06:00+00:00","dateModified":"2022-11-28T16:01:44+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/"},"wordCount":364,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg","articleSection":["SQL Server errors"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/","url":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/","name":"Database does not exist error in sp_MSforeachdb - T-SQL error","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg","datePublished":"2022-04-11T05:06:00+00:00","dateModified":"2022-11-28T16:01:44+00:00","description":"Fix the SQL Server error Database does not exist when executing the EXEC sp_MSforeachdb undocumented system stored procedure in T-SQL.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/01\/problem-1951987_1920.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/errors\/db-do-not-exists-error-sp-msforeachdb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Database does not exist error in sp_MSforeachdb"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=7190"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7190\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5543"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=7190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=7190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=7190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}