插入查询错误:列计数与第1行的值计数不匹配

时间:2017-02-06 20:26:22

标签: mysql coldfusion insert-into cfquery

我有一个cfquery,我循环遍历值以将多行插入数据库。我必须这样做,而不是在循环中生成多个INSERT查询,因为我需要INSERT之后的generatedKeys列表。但是我的查询现在抛出以下错误。我知道错误意味着什么,但根据我的查询它没有意义。

查询:

<cfquery datasource="#OLMSdatasourceWrite#" result="myResult">
    INSERT INTO OLMS_Data_RatioScenarios
    (
        OLMS_Account_ID,
        OLMS_RatioScenario_Name
    )
    VALUES
    (
    <cfset numItems = ListLen(AccountListWithSettings)>
    <cfset i = 1>

    <cfloop list="#AccountListWithSettings#" index="CurrentAccount">
        (<cfqueryparam cfsqltype="cf_sql_numeric" value="#CurrentAccount#" maxlength="255">
        , <cfqueryparam cfsqltype="cf_sql_clob" value="#requestBody.value#" maxlength="255">
        )
        <cfif i lt numItems>
            ,
        </cfif>

        <cfset i++>
    </cfloop>
    )
</cfquery>

<cfoutput>Inserted ID is: #myResult.generatedkey#</cfoutput>

错误:

Root Cause:java.sql.SQLException: Column count doesn't match value count at row 1
SQL: INSERT INTO OLMS_Data_RatioScenarios ( OLMS_Account_ID, OLMS_RatioScenario_Name ) 
VALUES ( ( (param 1) , (param 2) ) , ( (param 3) , (param 4) ) , ( (param 5) , (param 6) ) ) 

1 个答案:

答案 0 :(得分:0)

更正代码:

<cfquery datasource="#OLMSdatasourceWrite#" result="myResult">
    INSERT INTO OLMS_Data_RatioScenarios
    (
        OLMS_Account_ID,
        OLMS_RatioScenario_Name
    )
    VALUES
    <cfset numItems = ListLen(AccountListWithSettings)>
    <cfset i = 1>
    <cfloop list="#AccountListWithSettings#" index="CurrentAccount">
        (
          <cfqueryparam cfsqltype="cf_sql_numeric" value="#CurrentAccount#" maxlength="255">
         , <cfqueryparam cfsqltype="cf_sql_clob" value="#requestBody.value#" maxlength="255">
        )
        <cfif i lt numItems>
            ,
        </cfif>

        <cfset i++>
    </cfloop>
</cfquery>