{"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"},"_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}]}}