Possible bug: looping over queries within loops

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Possible bug: looping over queries within loops

    Don't know if this has been discussed already, but I am having problems with CF.

    I have one cfquery that I run that returns a recordset. I am looping over this
    using <cfloop query="queryName">. This happens fine.

    The problem is, is when I put ANOTHER query inside this loop, and then loop
    over that (again still inside the outer query loop), then inside the <cfloop>
    tag for the inner query, it can only see the first record in the first dataset.
    This is probably clearer if i demonstrate it.

    First, create two SQL tables, say TestTable1 and TestTable2. Populate the
    tables with arbitratry data:

    TestTable1:
    ----------------
    testKey1, data
    1,12
    2,34
    3,45

    TestTable2:
    ----------------
    testKey2, data
    1,67
    2,78
    3,34

    Now run this code on the tables:
    <cfquery name="getTestValues1" datasource="#ds#">
    SELECT
    data
    FROM
    testTable1
    </cfquery>

    <cfloop query="getTestValues1">
    data in outerloop - <cfoutput>#getTestValues1.data#</cfoutput><br>

    <cfquery name="getTestValues2" datasource="#ds#">
    SELECT
    data
    FROM
    testTable2
    </cfquery>

    <cfloop query="getTestValues2">
    data in innerloop - <cfoutput>#getTestValues1.data#</cfoutput>
    </cfloop>
    <br>
    </cfloop>

    The results you get are this:
    data in outerloop - 12
    data in innerloop - 12 data in innerloop - 12 data in innerloop - 12
    data in outerloop - 23
    data in innerloop - 12 data in innerloop - 12 data in innerloop - 12
    data in outerloop - 34
    data in innerloop - 12 data in innerloop - 12 data in innerloop - 12

    As you can see the value of #getTestValues1.data# in the outer loop is
    correct, but in the inner loop, it is only ever returned as the first record in
    its recordset.

    Cheers,
    Rob.

    RobinWhitehead Guest

  2. Similar Questions and Discussions

    1. problem looping through queries
      I am attempting to create a loop that will give me all records in a table whose ids are set to a certain value in a related table. The logic I am...
    2. For While Loops
      Hello, Please, can anyone tell me what is the equivalent in CMFL. Thanks Graham Brown
    3. Queries Of Queries Single Quote Problem
      When using queries of queries I'm having the following issue. Select Company_ID From qry_MyQuery Where Company_NM = 'MyString''s' <----...
    4. Possible Bug w/ nested loops and queries
      May have found a possible bug... can someone please verify? In a database I have two tables, temp1 and temp2 These tables can contain anything,...
    5. SQL OR CF loops?
      I'm hoping someone that actually knows what they are doing can assist me. I have a CFLOOP that simply spits out the contents of a table. Problem...
  3. #2

    Default Re: Possible bug: looping over queries within loops

    One of your problems is here.
    <cfloop query="getTestValues2">
    data in innerloop - <cfoutput>#getTestValues1.data#</cfoutput>
    </cfloop>

    When you are in a loop or cfoutput from one query, and want to work with
    another, you have to specify the row number in square brackets. So your code
    has to resmble
    <cfloop query="getTestValues2">
    data in innerloop - <cfoutput>#getTestValues1.data[rownumber]#</cfoutput>
    </cfloop>

    How you get the rownumber is up to you.


    Dan Bracuk Guest

  4. #3

    Default Re: Possible bug: looping over queries within loops

    This behavior is described in this technote
    [url]http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_17033[/url]

    It is a good idea to avoid nesting cfquery's inside a loop, for performance
    reasons. However, you could easily do this by using from/to loops.



    <CFQUERY NAME="getTestValues1" DATASOURCE="#ds#">
    SELECT Data FROM yourTable
    </CFQUERY>

    <cfloop from="1" to="#getTestValues1.recordCount#" index="outerRow">
    data in outerloop - <cfoutput>#getTestValues1.data[outerRow]#</cfoutput><br>
    <cfquery name="getTestValues2" datasource="#ds#">
    SELECT Data FROM yourTable
    </cfquery>
    <cfloop from="1" to="#getTestValues2.recordCount#" index="innerRow">
    data in innerloop - <cfoutput>#getTestValues2.data[innerRow]#</cfoutput>
    </cfloop>
    <br>
    </cfloop>

    mxstu Guest

  5. #4

    Default Re: Possible bug: looping over queries within loops

    But don't put the second query inside the first loop.
    Dan Bracuk Guest

  6. #5

    Default Re: Possible bug: looping over queries within loops

    As I said "It is a good idea to avoid nesting cfquery's inside a loop, for
    performance reasons." I would assume this is just a development test since a
    nested query with no WHERE clause doesn't usually make a lot of sense.

    mxstu Guest

  7. #6

    Default Re: Possible bug: looping over queries within loops

    But what I meant was, if you are going to nest query outputs, don't put the 2nd cfquery tag inside the first loop, or you will run it more than once.
    Dan Bracuk Guest

  8. #7

    Default Re: Possible bug: looping over queries within loops

    True, it would, but I am assuming that the test code structure is related to
    what the OP is really trying to do, which I am guessing involves nesting
    queries with some kind of WHERE clause. In which case they do want the query
    to execute more than once. This is why my example includes the cfquery within
    the loop, even though it really is not necessary in this case.
    That being said, I'll repeat my earlier disclaimer "nesting cfqueries within a
    loop should be avoided".

    Of course this is all just guessing until the OP provides more information ;-)


    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139