Introduction
In this article I have listed few methods to know about the list of database, tables, views,etc.., It will be very useful when we trace the database objects in the query window. Even though it can be accessible in the sql server object explorer, but when we write the query it can be customized. That means it can filter the result set based on our requirement.
How to list out the available database in the SQL Server current connection?
Method 1
- SP_DATABASES
Method 2
- SELECT name FROM SYS.DATABASES
Method 3
- SELECT name FROM SYS.MASTER_FILES
Method 4
- SELECT * FROM SYS.MASTER_FILES -- Type=0 for .mdf and type=1 for .ldf
The sp_databases is a system stored procedure it can be listed the database with the size.
The sys.databases will list the databases, created date, modified date and database id along with the other information
The SYS.MASTER_FILES will query the database details like the database id, size, physical storage path and list both mdf and ldf.
How to list the user tables in the database?
The following method can be used to get the list of user tables in the SQL server.
Method 1
- SELECT name FROM SYS.OBJECTS WHERE type='U'
Method 2
- SELECT NAME FROM SYSOBJECTS WHERE xtype='U'
Method 3
- SELECT name FROM SYS.TABLES
Method 4
- SELECT name FROM SYS.ALL_OBJECTS WHERE type='U'
Method 5
- SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
Method 6
- SP_TABLES
How to list out the Stored Procedures in the database?
Method 1
- SELECT name FROM SYS.OBJECTS WHERE type='P'
Method 2
- SELECT name FROM SYS.PROCEDURES
Method 3
- SELECT name FROM SYS.ALL_OBJECTS WHERE type='P'
Method 4
- SELECT NAME FROM SYSOBJECTS WHERE xtype='P'
Method 5
- SELECT Routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE='PROCEDURE'
The SYS.OBJECTS table has the common table that has the list for all the procedure, table, triggers, views,etc.., Here procedure can be filtered using the type='p'.
The Information_schema.routines is a view that has used in the SQL server 7.0 version. Now exclusive table available for the stored procedure.
How to list all Views in the database?
Method 1
- SELECT name FROM SYS.OBJECTS WHERE type='V'
Method 2
- SELECT name FROM SYS.ALL_OBJECTS WHERE type='V'
Method 3
- SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS
Method 4
- SELECT name FROM SYS.VIEWS
How to list out the Functions in the database?
Method 1
- SELECT name FROM SYS.OBJECTS WHERE type='IF' -- inline function
Method 2
- SELECT name FROM SYS.OBJECTS WHERE type='TF' -- table valued function
Method 3
- SELECT name FROM SYS.OBJECTS WHERE type='FN' -- scalar function
Method 4
- SELECT name FROM SYS.ALL_OBJECTS WHERE type='IF' -- inline function

Naresh Babu GopavaramPosted Apr 7, 2011, 5:36 AM
hi how to create stored procedure for generate existing table create script. in .net or in SQL server can u help me please......
Iqbal ButtPosted Mar 4, 2010, 9:24 PM
Very nice article senthilkumar.