Solved

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

Posted on 2016-09-06
2
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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