Solved

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

Posted on 2016-08-08
2
54 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
[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: 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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