Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to display the column names from a query.

Posted on 2003-11-10
2
Medium Priority
?
260 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 600 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 400 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

877 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