2009-08-12 34 views
0

CF8和MySQL5,用於在INSERT INTO中循環。sql語法錯誤,同時在cfquery中插入cfloery INSERT INTO

我正在獲取表單數據並試圖優化insert語句以在cfquery中循環。 最好的情況是,循環只是在VALUES附近,以便擁有一個INSERT,但我在嘗試識別第二個迭代時遇到了問題,並且在第一個INSERT VALUES的開頭放了一個逗號。 所以我用循環遍歷整個INSERT。至少在同一個cfquery中。我的問題是我無法得到正確的循環的第二個(和更多)迭代的語法。它對第一個工作正常,如果我把環路取出,工作正常。

我得到的錯誤是:

sql syntax error near 'INSERT INTO table(wID,session,xNameVar,xID,set1rt,set2rt,set3rt,set4rt,set5rt,set' 

這裏是我使用的代碼代碼:

<cfif structKeyExists(form, "x1s1xID")><!--- verifies if there is data in field 1 for a specific form section---> 
    <cfquery name="qCreateXdata" datasource="#application.datasource#"> 
    <cfloop from="1" to="#form.sessions#" index="i"><!--- form.sessions is a variable for the number of times to loop ---> 
     <cfif structKeyExists(form, "x1s#i#xID")><!--- a little redundant but used for other iterations of loop otherwise get a variable not defined error ---> 
     INSERT INTO table 
     (wID, 
     session, 
     xNameVar, 
     xID, 
     set1rt, 
     set2rt, 
     set3rt, 
     set4rt, 
     set5rt, 
     set6rt) 
     VALUES 
     (#variables.wID#, 
     #wCreateFormData["session" & i]#, 
     '#wCreateFormData["x1s" & i & "xNameVar"]#', 
     #wCreateFormData["x1s" & i & "xid"]#, 
     <!--- the below values are not required in form so need to be null integers instead of empty strings if not set in form ---> 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set1reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set1r" & i]))#" />, 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set2reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set2r" & i]))#" />, 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set3reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set3r" & i]))#" />, 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set4reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set4r" & i]))#" />, 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set5reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set5r" & i]))#" />, 
     <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x1set6reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x1set6r" & i]))#" /> 
     ) 
     <cfif structKeyExists(form, "x2s#i#xID")> 
      ,(#variables.wID#, 
      #wCreateFormData["session" & i]#, 
      '#wCreateFormData["x2s" & i & "xNameVar"]#', 
      #wCreateFormData["x2s" & i & "xid"]#, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set1reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set1r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set2reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set2r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set3reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set3r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set4reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set4r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set5reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set5r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x2set6reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x2set6r" & i]))#" /> 
      ) 
     </cfif> 
     <cfif structKeyExists(form, "x3s#i#xID")> 
      ,(#variables.wID#, 
      #wCreateFormData["session" & i]#, 
      '#wCreateFormData["x3s" & i & "xNameVar"]#', 
      #wCreateFormData["x3s" & i & "xid"]#, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set1reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set1r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set2reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set2r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set3reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set3r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set4reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set4r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set5reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set5r" & i]))#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#trim(wCreateFormData["x3set6reps" & i])#" maxlength="3" null="#NOT len(trim(wCreateFormData["x3set6r" & i]))#" /> 
      ) 
     </cfif> 

     <!---... you get the idea.....there are 12 total cfif blocks like this ---> 
     <!--- then I end with a ';' in order to close this INSERT statement and go back to the start of the loop for #form.sessions# number of times ---> 

     ; 
     </cfif> 
    </cfloop> 
    </cfquery> 
</cfif> 
+0

你爲什麼要做''而不是''?我的意思是 - 你*實際上試圖發出多個插入語句,不是嗎? – Tomalak 2009-08-12 13:29:17

+0

性能 - 嘗試避免每次循環迭代時cfquery的開銷。出於同樣的原因,最好我只是循環VALUES,但保持每個單獨的INSERT是第二好的。 – 2009-08-12 13:39:06

+0

無論如何,你正在通過'進行插入。你有沒有衡量這個實際影響有多大,還是更多的猜測? – Tomalak 2009-08-12 13:56:41

回答

2

我相信這能代替你的整個代碼:

<cfset i_max = 6> 

<cfloop from="1" to="#form.sessions#" index="s"> 
    <!--- do all the 12 blocks you speak of ---> 
    <cfloop from="1" to="12" index="x"> 
    <cfif StructKeyExists(form, "x#x#s#s#xID")> 

     <cfquery name="qCreateXdata" datasource="#application.datasource#"> 
     INSERT INTO table (
      wID, 
      session, 
      xNameVar, 
      xID, 
      <cfloop from="1" to="#i_max#" index="i"> 
      set#i#rt 
      <cfif i lt i_max>,</cfif> 
      </cfloop> 
     ) VALUES (
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#variables.wID#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#wCreateFormData["session" & s]#" />, 
      <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#wCreateFormData["x#x#s#s#xNameVar"]#" />, 
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#wCreateFormData["x#x#s#s#xID"]#" />, 
      <cfloop from="1" to="#i_max#" index="i"> 
      <cfqueryparam 
       cfsqltype = "CF_SQL_INTEGER" 
       value  = "#Trim(wCreateFormData["x#x#set#i#reps" & s])#" 
       maxlength = "3" 
       null  = "#Len(Trim(wCreateFormData["x#x#set#i#r" & s])) = 0#" 
      /> 
      <cfif i lt i_max>,</cfif> 
      </cfloop> 
     ) 
     ; 
     </cfquery> 

    </cfif> 
    </cfloop> 
</cfloop> 

提示:儘量減少複製和粘貼的編程,試着找到更多的抽象重複的模式。 ;-)做本質上是一樣的東西連續十二次通過複製粘貼應該引發一個大紅色的警報。

+0

我同意。因爲我試圖在規範化和空值數量之間取得平衡,所以我對此很懶惰。 – 2009-08-12 14:11:58

+0

你讓我99%的路上。非常感謝和感謝,我欣賞這一課! – 2009-08-13 00:59:49