views:

212

answers:

7

For the various popular database systems, how do you list all the columns in a table?

+2  A: 

For Oracle (PL/SQL)

SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'

For MySQL

SHOW COLUMNS FROM table_name
MattGrommes
Youd probably want to order the Oracle query by column_id
David Aldridge
+3  A: 

DESCRIBE name_of_table;

dave
A: 

MS SQL Server:

sp_columns [tablename]

Bryan
A: 

For MS SQL Server:

select * from information_schema.columns where table_name = 'tableName'
Jeff Meatball Yang
+2  A: 

SQL Server

SELECT 
    c.name 
FROM
    sys.objects o
INNER JOIN
    sys.columns c
ON
    c.object_id = o.object_id
AND o.name = '[Table Name]'

or

SELECT 
    COLUMN_NAME 
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME  = '[Table Name]'

The second way is an ANSI standard and therefore should work on all ANSI compliant databases.

Russ Cam
A: 

SQL Server

To list all the user defined tables of a database:

use [databasename]
select name from sysobjects where type = 'u'

To list all the columns of a table:

use [databasename]
select name from syscolumns where id=object_id('tablename')
Mircea Grelus
Heh? This is just wrong...you can only use USE for databases...And the query returns all user defined tables in the database, which is not what the OP wanted.
Maximilian Mayerl
Ah, sorry. I've corrected it.
Mircea Grelus
A: 

Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):

SELECT name
  FROM sys.columns 
  WHERE [object_id] = OBJECT_ID('dbo.tablename');
Aaron Bertrand