Jump to content

Archived

This topic is now archived and is closed to further replies.

yeago

How to return a table's column names?

Recommended Posts

I am attempting to get the microsoft SQL equivalent of "show columns from table" from MySQL.

HOW?

Share this post


Link to post
Share on other sites
Here's one way

[code]$result = mssql_query("SELECT TOP 1 * FROM tablename");
$row = mssql_fetch_assoc($result);
foreach ($row as $colname => $value) {
    echo $colname . '<BR />';
}[/code]

Share this post


Link to post
Share on other sites
You can use a query like this:

[code]select * from information_schema.columns where table_name = '<<your table name>>'[/code]

Share this post


Link to post
Share on other sites
Continuing with GeoffOs post, I find it helpful to include the database name in the FROM clause.

just do a select on the information_schema.COLUMNS table:

select column_name
from <dbname>.information_schema.columns
where table_name = '<tablename>'

see: [a href=\"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ia-iz_4pbn.asp\" target=\"_blank\"]http://msdn.microsoft.com/library/default...._ia-iz_4pbn.asp[/a]

Cheers!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.