Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-06
2
Medium Priority
?
65 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

636 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