Dynamically Appending A Column To A Query In ColdFusion

Published: {ts '2015-03-17 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/dynamically-appending-a-column-to-a-query-in-coldfusion/

Since my life has been somewhat how do I say this... crazy the last few months I've not had much time for blogging. I'm going to try rectify that and get back in the habit. To start off though I'm going to cheat just a little bit by finishing up a draft I've had sitting around for a couple of years now.

Premise: How do you add an extra column to an existing query?

Wrong Way: Using Query of Query

I see this a lot and it does work.

SELECT firstName , lastName , (firstName + ' ' + lastName) AS fullName FROM getArtists

The problem I see with this though is that it requires starting up the QoQ engine which if not already started takes time.

Right Way: Using QueryAddColumn()

A better way of doing this is to just manipulate the query object directly like this.

Now the results are essentially the same and the performance difference is minimal and getting smaller on newer versions of CF, but I still hold that the second option is better.