Solved

The following query doesn't list all the databases in the server

Posted on 2016-09-06
2
44 Views
Last Modified: 2016-09-06
Heyas,

The following query:

DECLARE @command varchar(1000)
select @command = 'USE ?; select DB_NAME(), default_schema_name
, type_desc
, name
, create_date
from sys.database_principals
order by default_schema_name
, type_desc'
EXEC sp_MSforeachdb @command

Doesn't list all the database on the server I am running it on. I do get a one error message: Msg 911, Level 16, State 1, Line 1
Database 'Chris21PROD' does not exist. Make sure that the name is entered correctly.

How do make the query list all the databases in the server, any assistance is appreciated?

Thank you.
0
Comment
Question by:Zack
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41787091
The sp_MSforeachdb is undocumented. It uses a wrong table when builds a cursor. As a result it gives you wrong results.

Read here (there is a big article search for sp_MSforeachdb). Aaron recommends stopping using this SP. He has provided an example of his own version of the sp. Switch to this version instead
0
 

Author Closing Comment

by:Zack
ID: 41787127
Cheers for the info I will use Aarons SP.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question