Solved

How to display the column names from a query.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Time Sheet Help 6 44
IIS7 - programmatically adding new bindings without recycling site - or any other automated way? 2 91
listing all functions in JavaScript 19 172
WEB Farm 6 59
A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

12 Experts available now in Live!

Get 1:1 Help Now