Solved

system tables join

Posted on 2012-03-12
4
554 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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

12 Experts available now in Live!

Get 1:1 Help Now