Solved

How to list the database name alongside the current corresponding query results

Posted on 2016-08-08
2
38 Views
Last Modified: 2016-08-08
Heyas,

I have the following query:

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

Works fine.

My query is how to list the corresponding database for each of these existing result tests.

Any assistance is appreciated.

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

Accepted Solution

by:
chaau earned 500 total points
ID: 41746758
use DB_NAME():

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 

Open in new window

0
 

Author Closing Comment

by:Zack
ID: 41746765
Thank you just saved me a few hrs :). If you are able can you offer any insight into this query:

https://www.experts-exchange.com/questions/28961401/Configuring-a-maintenance-plan-or-scheduled-jobs-to-via-rules-set-by-the-resource-governor-Error-OLE-DB-error-code-0x80040E4D-Login-failed-for-user-'SQL-RG-MP-User'.html  

I have scoured google but I can't any meaning insights into my query.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

17 Experts available now in Live!

Get 1:1 Help Now