Solved

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

Posted on 2016-09-06
2
32 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
Comment Utility
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
Comment Utility
Cheers for the info I will use Aarons SP.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server memory Issue 7 73
SQL JOIN 6 31
SQL Help - 12 34
SQL Update Query - What's wrong with this. 18 8
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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now