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
2009-10-16 21:11:29
Youd probably want to order the Oracle query by column_id
David Aldridge
2009-10-18 12:09:03
A:
For MS SQL Server:
select * from information_schema.columns where table_name = 'tableName'
Jeff Meatball Yang
2009-10-16 21:17:47
+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
2009-10-16 21:19:35
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
2009-10-16 21:20:53
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
2009-10-16 21:25:25
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
2009-10-16 22:59:38