Solved

system tables join

Posted on 2012-03-12
4
581 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 2 29
sql server concatenate fields 10 31
Sql server function help 15 27
Sql Server group by 10 23
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 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

8 Experts available now in Live!

Get 1:1 Help Now