views:

57

answers:

5

used is c# sql vs 08 sql server 2005 express

whenever and where ever an sql select statement is used, its always like

select  * from tablename

or count statement is alsi like

select count something from table name

for selecting or doing anything on the tables,

i would like to know which tables exits in my the database i am connected to!

so like

select alltablenames from database_name.

please guide.

+1  A: 

to list all table of database

USE YourDBName
GO 
SELECT *
FROM sys.Tables WHERE type='u' 

to check table exists in database or not

IF EXISTS (SELECT 1 
    FROM sysobjects 
    WHERE xtype='u' AND name='tablename') 
        SELECT 'tablename exists.' 
ELSE 
        SELECT 'tablename does not exist.'
Pranay Rana
A: 

The following query returns the names of the tables in an SQL Server database:

select name from sysobjects where xtype = 'U'
Fredrik Mörk
A: 

See http://database.ittoolbox.com/documents/finding-table-names-in-sql-18556 - simple queries for all table names and all columns for a given table.

Reddog
+3  A: 

Personally, I would use the Information_Schema.Tables & Information_Schema.Columns views as these are views provided by Microsoft. (Rather than using the sysobjects tables)

Barry
The reason to use them IMO is more that they're views which are part of the ANSI standard, shouldn't change between versions on a whim, and should work the same in other ANSI compliant DBs - in other words, because they're NOT Microsoft specific. sysobjects is a Microsoft specific solution.
Tom H.
A: 

or

EXEC sp_tables
Madhivanan