×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Getting table names from sysobjects

Getting table names from sysobjects

Getting table names from sysobjects

(OP)
How can I get a list of all tables in the db in the following format?

servername.dbname.dbo.tablename

so far I have
select name from sysobjects where type='U'

that returns only names of tables os i need servername and dbname

thanks!

RE: Getting table names from sysobjects

There is no central table which has a list of all the tables in every database on the server.  You need to query the sysobjects table in each database to get the information.

This code will do the trick.

CODE

create table #tables
(database_name sysname,
owner sysname,
table_name sysname)

exec sp_MSforeachdb 'use ?
insert into #tables
select db_name(), sysusers.name, sysobjects.name
from sysobjects
join sysusers on sysobjects.uid = sysusers.uid
where sysobjects.xtype = 'U''

select @@SERVERNAME + '.' + database_name + '.' + owner + '.' + table_name
from #tables

drop table #tables

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible.  All it takes is a little research. (Me)
noevil
http://www.mrdenny.com

RE: Getting table names from sysobjects

(OP)
this is awesome! What do I need to do to only get table on 1 database (the db on which this is run?)

thanks

RE: Getting table names from sysobjects

To get the tables from a single database.

CODE

select @@SERVERNAME + '.' + db_name() + '.' + sysusers.name + '.' + sysobjects.name
from sysobjects
join sysusers on sysobjects.uid = sysusers.uid
where sysobjects.xtype = 'U'

You can run the above code from any database.  The sp_MSforeachdb procedure uses a cursor to get data from every database.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible.  All it takes is a little research. (Me)
noevil
http://www.mrdenny.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close