Skip to main content
July 22, 2009
Question

Update with CFLoop

  • July 22, 2009
  • 1 reply
  • 2480 views

People,

I need to update a field with a sequence number (1,2,3,4,5...) but, my code is updating all rows only with the last value... (10).

See the code below;

<!---<cfquery name="q" datasource="x">
            select *
            from table1
            where id=#attributes.id#
            order by data_vencimento
        </cfquery>           
       
            <cfloop query ="q">           
           
            <cfset idx = 0>
                                  
            <cfloop  index="idx" from="1" to="#q.recordcount#">
           
                <cfset idx = idx+1>
           
               
                <cfquery name="q2" datasource="x">
               
                UPDATE table1
                SET column=<cfqueryparam value="#idx#" cfsqltype="cf_sql_integer">
                where id=#attributes.id#
               
            </cfquery>
           
            </cfloop>--->

What's wrong here?

Tks!

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    July 22, 2009

    There is nothing in your update query to distinguish the individual records.

    I have no idea what you are attempting to accomplish, but this is how I read your code and I find its purpose quite confusing.

    Read a set of records where id = #attributes.id#

    <cfquery name="q" datasource="x">
      select *
      from table1
      where id=#attributes.id#
      order by data_vencimento
    </cfquery>

    Iterate over each of the records in this set.

    <cfloop query ="q">

    Each record initilze a counter to zero.

    <cfset idx = 0>

    Loop from 1 to the number of records in the record set, reusing the previously initlized counter.

    <cfloop  index="idx" from="1" to="#q.recordcount#">

    Add one to this counter|loop Index, effectivily jumping the loop counter ahead by one???

    <cfset idx = idx+1>

    Update all the records in the record set to the latest value in the counter|loop index variable

    <cfquery name="q2" datasource="x">
      UPDATE table1
      SET column=<cfqueryparam value="#idx#" cfsqltype="cf_sql_integer">
      where id=#attributes.id#
    </cfquery>
    July 22, 2009

    Hello Ian Skinner,

    I need update a table like that...

    IDLanc     Parc

    1310        1

    1311        8

    1312        4

    1313        2

    1314        9

    I need to update the column Parc using a sequence of numbers and need to be like above...

    IDLanc     Parc

    1310        1

    1311        2

    1312        3

    1313        4

    1314        5

    I trying to do that...

    Inspiring
    July 22, 2009

    The currentrow variable that comes with cfquery looks like it might help.