2

I have a need to iterate over all of the tables in my database. I am doing so with thsi query:

SELECT so.name, so.*
FROM sysobjects so
WHERE so.xtype = 'U'

This works fine for all of the tables in the 'dbo' schema, but it doesn't work for any other schemas. I want the output to be like this:

Users.Address Users.TelephoneNumbers dbo.GlobalSettings dbo.Configuration

Instead I get this:

Address TelephoneNumber GlobalSettings Configuration

When I try to construct dynamic SQL queries, it fails because it can't find the Address and TelephoneNumber tables because their schema is not the default 'dbo'.

Any ideas?

2
  • What version of SQL Server? YOu can use SYSTABLES: msdn.microsoft.com/en-us/library/ms187406.aspx Commented Dec 9, 2009 at 20:39
  • Is this Sql Server 2000 (I believe it is from the SQL you posted)? Commented Dec 9, 2009 at 20:39

3 Answers 3

4

How about:

Select Schema_Name + '.' + Table_Name from Information_Schema.Tables
Sign up to request clarification or add additional context in comments.

3 Comments

@OMG Ponies - No, information_schema.tables will work on SQL Server. You can refer to this link: msdn.microsoft.com/en-us/library/ms186224.aspx
+1: Sorry about that, I stand corrected & withdraw my earlier statement
It's important to give Microsoft credit when they adhere to standards :)
2

In SQL Server 2005 and up, you should no longer use the deprecated sysobjects and sytables and these views.

Use the "sys" catalog views instead:

SELECT * FROM sys.tables

should work on other than the "dbo" schema as well.

Comments

0

In SQL Server 2005/2008:

SELECT * FROM information_schema.tables

In SQL Server 2000:

SELECT * FROM sysobjects WHERE xtype='U'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.