Solved

How to display the column names from a query.

Posted on 2003-11-10
2
258 Views
Last Modified: 2013-12-24
OK, here is the problem.

I have a query:

<cfquery name="AllListings" datasource="Home4SaleByMe" cachedwithin = "#CreateTimeSpan(0, 6, 0, 0)#">
SELECT * FROM user975947.Listing ORDER BY #SortBy#
</cfquery>

Now I never know how many columns there are, so I want to be able to loop through and list the actual column names from the database, like this (or something similar - you get the point).  I would also like to see if the column equals a known name and do something.:

<cfloop ...>
  <cfif AllListings.columnName(i) = "ID">
      This is the ID Column
  <cfelse>
    #AllListings.columnName(i)#
 </cfif>
</cfloop>
0
Comment
Question by:turbosig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
danrosenthal earned 150 total points
ID: 9719071
This should do it:

<cfquery name="AllListings" datasource="Home4SaleByMe" cachedwithin = "#CreateTimeSpan(0, 6, 0, 0)#">
SELECT * FROM user975947.Listing ORDER BY #SortBy#
</cfquery>

<CFLOOP list="#AllListings.columnlist#" index="column_name">
      <CFIF column_name eq "ID">
            This is the ID column
      <CFELSE>
            <CFOUTPUT>#column_name#</cfoutput>
      </cfif>
      <BR>
</cfloop>
0
 
LVL 2

Assisted Solution

by:jonnygo55
jonnygo55 earned 100 total points
ID: 9719091
Assuming you know the name of the table you could do this:

<cfquery name="columns" datasource="Home4SaleByMe">
SELECT     sc.name
FROM         sysobjects so INNER JOIN
                      syscolumns sc ON so.id = sc.id
WHERE     (so.name = '#yourTableName#') AND (sc.autoval IS NULL)
ORDER BY sc.colid
</cfquery>

<cfoutput query="columns">
  <cfif columns.name = "ID">
        This is the ID Column
    <cfelse>
      #columns.name#
   </cfif>
</cfoutput>
0

Featured Post

Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
What You Need to Know when Searching for a Webhost Provider
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question