1

I am using SQl Server 2014, and Normally we can list out tables if we knew the DB name as :

USE YOURDBNAME
GO 
SELECT *
FROM sys.Tables
GO

But I want to know all the tables irrespective of db's that are present on my machine

Or can I go for looping by listing out all DB name.(EXEC sp_databases)

Is there any better way to find out this?

3
  • select * from information_schema.tables? Commented Dec 11, 2017 at 9:54
  • @jarlh No man.. It just shows master DB tables . What I get with select count(*) from information_schema.tables = 6 where as select * from <anyDB>.information_schema.tables = 87. So this is not the Answer to list all the tables within my machine Commented Dec 11, 2017 at 9:56
  • SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U' Commented Dec 11, 2017 at 10:03

3 Answers 3

1

There are several ways of getting all tables in your database

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

OR

SELECT * FROM Sys.Tables

OR

SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U'

Docs for all the xtypes

Sign up to request clarification or add additional context in comments.

Comments

0
select * from tab

or

select * from * cat

The first one will show the associated CLUSTERID with tables for the current user.

Comments

0

I think you need

SELECT name FROM master.sys.databases

and then you will need to create a cursor query (for your USE XXXX) to iterate through the following query.

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

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.