Connecting Tech Pros Worldwide Forums | Help | Site Map

How to I create blank columns using QueryAddColumn?

Haitashi's Avatar
Member
 
Join Date: Jun 2007
Location: Orlando, FL
Posts: 94
#1: Mar 2 '09
Hi:

I need to dymanically create a number of columns using the queryAddColumn function.

I was going to use the loop below. That way each column would have a generic dynamically created name.

Expand|Select|Wrap|Line Numbers
  1. <cfloop index="I" from="1" to="#Variables.qProgramDetails.recordcount#">
  2.     <cfset temp = QueryAddColumn(#this.RecordSet#, "Detail#I#", "VarChar", newArray) />
  3. </cfloop>
The problem I'm encountering is that when running that code I get an error that says: "The column name (Detail1) that you specified already exists in this query".

I'm guessing something isn't quite right since apparently on the second iteration of this loop the column name isn't updating to "Detail2".

Any ideas? As always, thanks in advance!! =)

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Mar 4 '09

re: How to I create blank columns using QueryAddColumn?


...or perhaps the query already has a column named Detail1. Is this.RecordSet an empty query before the loop?
Newbie
 
Join Date: Mar 2009
Posts: 2
#3: Mar 9 '09

re: How to I create blank columns using QueryAddColumn?


Try this:

Expand|Select|Wrap|Line Numbers
  1. <cfloop ....>
  2.  
  3.    <cfset col = "Detail"&#I#>
  4.    <cfset temp = QueryAddColumn(#this.RecordSet#, "#col#", "VarChar", newArray) />
  5.  
  6. </cfloop>
Or try dynamic variables - lookup Evaluate function.

Good luck - CM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Mar 10 '09

re: How to I create blank columns using QueryAddColumn?


cymark, welcome to Bytes and thanks for your contribution.

You could do away with the pound signs in the cfset statement, i.e.
Expand|Select|Wrap|Line Numbers
  1. <cfset col = "Detail"& I>
In this case, however, the code that you've posted won't make a difference because it does recognise "#I#" to be 1 (see error message).
Newbie
 
Join Date: Mar 2009
Posts: 2
#5: Mar 10 '09

re: How to I create blank columns using QueryAddColumn?


Hi,

Sorry was a bit quick there.

The following seems to work OK:

Expand|Select|Wrap|Line Numbers
  1. <cfset myQuery = QueryNew("")>
  2.  
  3. <cfset FastFoodArray = ArrayNew(1)>
  4. <cfset FastFoodArray[1] = "French Fries">
  5. <cfset FastFoodArray[2] = "Hot Dogs">
  6. <cfset FastFoodArray[3] = "Fried Clams">
  7. <cfset FastFoodArray[4] = "Thick Shakes">
  8.  
  9.  
  10. <cfloop index="I" from="1" to="5">
  11.  
  12.     <cfset col = "Detail"&I>
  13.  
  14.      <cfset temp = QueryAddColumn(#myQuery#, #col#, "VarChar", FastFoodArray) />
  15. </cfloop>
  16.  
  17.  
  18. <cfdump var="#myQuery#">
Creates cols with Detail1, .. to Detail5

Hope it helps...
Reply

Tags
blank column, coldfusion, dynamic column, queryaddcolumn