在cfquery INSERT INTO中cflooping时出现sql语法错误

时间:2009-08-12 12:44:38

标签: sql mysql coldfusion

CF8和MySQL5,用于INSERT INTO中的循环。

我正在获取表单数据并尝试优化insert语句以在cfquery中循环。 最好的情况是循环只是在VALUES周围才能有一个INSERT,但我在尝试ID第二次迭代并在第一个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>

1 个答案:

答案 0 :(得分: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>

提示:减少复制和粘贴编程,尝试找到重复模式的更多抽象。 ;-)通过复制和粘贴连续十二次执行基本相同的事情应该会引发一个大红色警报。