Thane,

No need to have a separate "CommitWrite" variable. Just do this:

<cftransaction><!--- Begin is implied --->
<cftry>
        <cfloop>
        ...DB Inserts here...
        </cfloop>
<cfcatch type="Database">
        <cftransaction action="rollback"/>
</cfcatch>
</cftry>

</cftransaction><!--- Commit is implied --->

Provided you want all inserts to be performed or none at all...otherwise
you need to adjust where the loop is etc.

André

-----Original Message-----
From: Thane Sherrington [mailto:[EMAIL PROTECTED]] 
Sent: 18 February 2003 14:43
To: CF-Talk
Subject: CFTransaction and looping through multiple inserts

I'm entering a series of answers into a table, so I have a loop like
this:

<cfloop index="QuestionID" list="#Form.SurveyQuestionIDList#">
        <!--- Get the answer. --->
        <CFSET NumericAnswer=Form["Answer"&QuestionID]>
        <!--- Run the query. --->
        <cfquery datasource="#Datasource#" name="InsertSurveyAnswers">
                INSERT INTO     SurveyAnswers(QuestionID,
SurveySubjectID, 
NumericAnswer,          SurveyDate)
                VALUES          (#QuestionID#, #Form.SubjectID#, 
#NumericAnswer#,                #Form.DateSurveyEntered#)
        </cfquery>
</CFLOOP>

If I want to use CFTransaction, would I set it up like this?

<CFSET CommitWrite=True>
<cftransaction action=begin>
        <cftry>
        <cfloop index="QuestionID" list="#Form.SurveyQuestionIDList#">
                <!--- Get the answer. --->
                <CFSET NumericAnswer=Form["Answer"&QuestionID]>
                <!--- Run the query. --->
                <cfquery datasource="#Datasource#"
name="InsertSurveyAnswers">
                        INSERT INTO     SurveyAnswers(QuestionID,
SurveySubjectID,                        NumericAnswer, 
SurveyDate)
                        VALUES          (#QuestionID#, #Form.SubjectID#,

#NumericAnswer#,                        #Form.DateSurveyEntered#)
                </cfquery>
        </CFLOOP>
        <cfcatch type='database'>
            <cfset CommitWrite=FALSE>
        </cfcatch>
   </cftry>

<cfif CommitWrite is 'TRUE'>
     <cftransaction action='Commit'>
   <cfelse>
     <cftransaction action='Rollback'>
   </cfif>

</cftransaction>

Or like this?

<CFSET CommitWrite=True>
<cftransaction action=begin>
        <cfloop index="QuestionID" list="#Form.SurveyQuestionIDList#">
        <!--- Get the answer. --->
        <CFSET NumericAnswer=Form["Answer"&QuestionID]>
        <!--- Run the query. --->
        <cftry>
                <cfquery datasource="#Datasource#"
name="InsertSurveyAnswers">
                        INSERT INTO     SurveyAnswers(QuestionID,
SurveySubjectID,                        NumericAnswer, 
SurveyDate)
                        VALUES          (#QuestionID#, #Form.SubjectID#,

#NumericAnswer#,                        #Form.DateSurveyEntered#)
                </cfquery>
        <cfcatch type='database'>
            <cfset CommitWrite=FALSE>
        </cfcatch>
          </cftry>
        </CFLOOP>


<cfif CommitWrite is 'TRUE'>
     <cftransaction action='Commit'>
   <cfelse>
     <cftransaction action='Rollback'>
   </cfif>

</cftransaction>

Or can CFTransaction be used in this situation?

T


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to