Skip to main content
Participant
August 26, 2009
Question

Column names from query

  • August 26, 2009
  • 1 reply
  • 2467 views

Hello Everyone,

I need to have a list of all the column names along with data. I found that with query_name.columnlist gives the list, but in sorted order. I want it as it is. Another question, if i fire a query with * ( select * ), how to refer to column, as i'm not aware of column names. Is there any way that query result can be stored in array then reference via index. eg; query_name[0] would give first column.

Thanks,

Tushar Saxena

This topic has been closed for replies.

1 reply

Inspiring
August 26, 2009

If you are not using select *, you can set a variable with your column names.  Then in your query, you have:

select #yourcolumns#

from etc

to output the data, use array notation.  Something like.

<cfoutput query = "yourquery">

<cfloop list = "#yourcolumns#" index = "column">

#yourquery[column]#

If you are using select *, and you are selecting from one table only, you may be able to query your db's system tables to get the column names in the order you want them.  If you are selecting from more than one table, using select * is a bad idea because you are bring back at least one column you don't need.

Participant
August 27, 2009

Thanks Dan! Still have a doubt that how to reference column names if using select *. Even if i could get column names in other vars. I'm new to CF so question may be silly.

getting column names:-

<cfquery datasource="RTW_ORA" name="cn">
SELECT COLUMN_NAME
FROM ALL_COL_COMMENTS
WHERE TABLE_NAME = '#meas#'
</cfquery>

getting data:-

<cfquery datasource="RTW_ORA" name="cd">
SELECT *
FROM #meas#
</cfquery>

How to output all the data?

Thanks!

Inspiring
August 27, 2009

Two of your freinds are <cfdump> and ValueList().  Dump your first query and see if it gives you anything useful.  If so, you can use ValueList to create the necessary variable.  Or you can use valuelist as the necessary variable.