?
Solved

system tables join

Posted on 2012-03-12
4
Medium Priority
?
671 Views
Last Modified: 2012-03-12
Hi

I have joined  sys.objects and  syscomments on ON syscomments.ID = sys.objects.object_id

I would now like to join to sysdatabases...can anyone tell me how please?
0
Comment
Question by:ac_davis2002
[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
4 Comments
 
LVL 25

Assisted Solution

by:jogos
jogos earned 1336 total points
ID: 37709145
sys.sysdatabases lists all databases on that instance.
sys.objects lists all objects in 1 database, so the databasename for that is always the same.

Current db you can get by DB_NAME() or DB_ID()

So when you are in database Adventurworks2008 getting your join

inner join sys.sysdatabases AS sdb ON sdb.dbid = DB_ID()
0
 
LVL 15

Assisted Solution

by:AmmarR
AmmarR earned 664 total points
ID: 37709168
hi ac_davis2002

its not a straight forward join

because sys.objects is objects of a single database

you usually use it this way

Use DB
select * from sys.objects

to get you all objects to a certain DB

so if you want to get all objects of every DB, you need to loop into each DB and run sys.objects and then combine your results

you can use the stored procedure sp_msforeachdb
http://stackoverflow.com/questions/8363430/select-all-databases-in-sysobjects-that-have-a-table-named-mytable-regardless

or you can use a nested select statment
http://stackoverflow.com/questions/7265350/find-table-name-in-all-objects-of-all-databases



here is a simple script below (its a lazy way to get sys.objects and add dbname)


//this code below is just to create a temp table to have similar no of columns of sys.objects
use master
select * into #temp from sys.objects
truncate table #temp

//here is just add another column called DBname to hold database name
Alter table #temp
add DBName varchar(100) 

//loop through Databases and get sys.objects
insert into #temp 
EXEC sp_msforeachdb [select  *,"?" AS db from sys.objects]


select * from #temp

Open in new window

0
 
LVL 25

Accepted Solution

by:
jogos earned 1336 total points
ID: 37709376
Both comments say the same , you want to combine 2 different levels of information we only go from different interpretation of the reason for your join
- my sollution : adds the db-info to the list of what you had in your query on sys.objects
- AmmarR : gives you the result for all your databases in your instance, but not a join with the info of your sys.sysdatabases-table


A little more efficiency in the sollution of AmmarR
select *,cast(' ' as varchar(100)) As DBName
 into #temp 
from sys.objects 
inner join sys.sysdatabases AS sdb ON sdb.dbid = DB_ID()
 where 1 = 2

Open in new window

=> not insterted records that need to be deleted(truncate) again and no alter table necessary

And don't forget the at the end
DROP TABLE  #temp

Open in new window


when you want to test it different times without error on the second try

And you probably not intrested in the objects of the system databases

EXEC master..sp_MSForeachdb '
IF ''?'' <> ''master'' AND ''?'' <> ''model'' AND ''?'' <> ''msdb'' AND ''?'' <> ''tempdb''
BEGIN
	select  *,"?" AS db 
        from sys.objects
        inner join sys.sysdatabases AS sdb ON sdb.dbid = DB_ID(''?'')
END
'

Open in new window

And notice that here I had to add the name of the database ('?' in with using sp_MSForeachdb) to the DB_ID() function or you always had the 'master' because that is the db-context in which you run your query.
0
 

Author Closing Comment

by:ac_davis2002
ID: 37710095
Thanks
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

770 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