Jump to content


How to return a table's column names?

  • Please log in to reply
3 replies to this topic

#1 yeago

  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 08:03 PM

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


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 May 2006 - 11:36 PM

Here's one way

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 GeoffOs

  • Members
  • PipPip
  • Member
  • 24 posts
  • LocationCheshire, England

Posted 17 May 2006 - 03:47 PM

You can use a query like this:

select * from information_schema.columns where table_name = '<<your table name>>'

Beyond a critical point within a finite space, freedom diminishes as numbers increase....[br]The human question is not how many can possibly survive within the system, but what kind of existence is possible for those who do survive."[br]-- Frank Herbert - Dune

#4 fractil

  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 June 2006 - 03:07 PM

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]

"One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs"

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users