質問

How do I use CFLOOP for updating my records on Access DB? I have tried using the code below but it seems it gives me a duplicated value instead of an increment

My code:

<cfloop index="i" from="1" to="3">
<cfquery name="query" datasource="datasource">
    update mytable
    set
    columnB = #i#
    where columnA = 'a'                                          
   </cfquery>
</cfloop>

Output of records under columnB= all number 3, it should be 1,2,3

My table looks like this....

Column A|ColumnA1|ColumnB
A         A        
A         B
A         C

Columns A and A1 is filled with a previous CFLOOP and SQL Insert.

役に立ちましたか?

解決

You're updating all the records that have columnA = 'a' each time you run the update, you need more criteria in your where clause to stop updating multiples rows at once.

You can easily see how many rows were updated by using the result attribute of cfquery, similar to the code below.

<cfloop index="i" from="1" to="3">
<cfquery name="query" datasource="datasource" result='updated'>
    update mytable
    set
    columnB = #i#
    where columnA = 'a'                                          
   </cfquery>
   <cfdump var='#updated#' />
</cfloop>

Update: Given your updated question, an example updated query may look like so, as the combination of columnA and ColumnA1 make the row unique.

<cfloop index="i" from="1" to="3">
<cfquery name="query" datasource="datasource" result='updated'>
    update mytable
    set
    columnB = #i#
    where columnA = 'a'      
    AND ColumnA1 = <cfqueryparam value='#i#' />                                    
   </cfquery>
   <cfdump var='#updated#' />
</cfloop>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top