How to I create blank columns using QueryAddColumn?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haitashi
    New Member
    • Jun 2007
    • 96

    How to I create blank columns using QueryAddColumn?

    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.

    Code:
    <cfloop index="I" from="1" to="#Variables.qProgramDetails.recordcount#">
        <cfset temp = QueryAddColumn(#this.RecordSet#, "Detail#I#", "VarChar", newArray) />
    </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
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    ...or perhaps the query already has a column named Detail1. Is this.RecordSet an empty query before the loop?

    Comment

    • cymark
      New Member
      • Mar 2009
      • 2

      #3
      Try this:

      Code:
      <cfloop ....>
          
         <cfset col = "Detail"&#I#>
         <cfset temp = QueryAddColumn(#this.RecordSet#, "#col#", "VarChar", newArray) />
      
      </cfloop>
      Or try dynamic variables - lookup Evaluate function.

      Good luck - CM
      Last edited by acoder; Mar 10 '09, 01:11 PM. Reason: Added [code] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        cymark, welcome to Bytes and thanks for your contribution.

        You could do away with the pound signs in the cfset statement, i.e.
        Code:
        <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).

        Comment

        • cymark
          New Member
          • Mar 2009
          • 2

          #5
          Hi,

          Sorry was a bit quick there.

          The following seems to work OK:

          Code:
          <cfset myQuery = QueryNew("")>
          
          <cfset FastFoodArray = ArrayNew(1)>
          <cfset FastFoodArray[1] = "French Fries">
          <cfset FastFoodArray[2] = "Hot Dogs">
          <cfset FastFoodArray[3] = "Fried Clams">
          <cfset FastFoodArray[4] = "Thick Shakes">
          
          
          <cfloop index="I" from="1" to="5">
          
          	<cfset col = "Detail"&I>
          	
               <cfset temp = QueryAddColumn(#myQuery#, #col#, "VarChar", FastFoodArray) />
          </cfloop>
          
          
          <cfdump var="#myQuery#">
          Creates cols with Detail1, .. to Detail5

          Hope it helps...
          Last edited by acoder; Mar 11 '09, 04:35 PM. Reason: Added [code] tags

          Comment

          Working...