Solved

system tables join

Posted on 2012-03-12
4
595 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
  • 2
4 Comments
 
LVL 25

Assisted Solution

by:jogos
jogos earned 334 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 166 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 334 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

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.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 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