Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Exploring database structure.

Author  Topic 

alan.curtis
Starting Member

2 Posts

Posted - 2008-07-14 : 15:37:46
I am accessing a database using sqlcmd and I don't know it's structure. What are the queries to list the tables in a database and fields in a table?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-14 : 15:39:38
You can use the INFORMATION_SCHEMA views.

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES

SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName'
ORDER BY ORDINAL_POSITION

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-15 : 03:42:24

Also

EXEC sp_columns 'table_name'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-15 : 06:35:11
More details on INFORMATION SCHEMA views here:-
http://msdn.microsoft.com/en-us/library/ms186778.aspx
Go to Top of Page
   

- Advertisement -