Solved

How to display the column names from a query.

Posted on 2003-11-10
2
250 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
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now