1

我收到 SELECT Scope_Identity() 错误

我正在从表单中插入一行新数据。该行生成一个自动增强的 ID(RBID)。

我试图立即抓住它并将它(RBID)插入另一行,以便在需要时可以引用它。

<cfquery name="recbilling" datasource="#request.dsn#" result="myResult">
INSERT INTO RBilling
(CID, UserID, DateCompleted, ClientID, Title, Qty, Rate, Taxable, Description, NextBillingDate,
FrequencyUnit, FrequencyDuration, ProjectID)
VALUES (
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">, 
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">, 
<cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">, 
<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">, 
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#NextBillingDate#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyUnit#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyDuration#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">)
</cfquery>

<cfquery name="getRBID" datasource="#request.dsn#">
SELECT Scope_Identity() as newID
</cfquery>
<cfset RBID = #getRBID.newID#>
</cfif>




<cfquery name="submithourssubmit" datasource="#request.dsn#">
INSERT INTO Billedfees
(CID, UserID, DateCompleted, ClientID, Title, Qty, Rate, Taxable, Description, ProjectID, RBID)
VALUES (
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">,
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">, 
<cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">, 
<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#RBID#">
)
</cfquery>

我得到的错误是:执行数据库查询时出错。[Macromedia][SQLServer JDBC 驱动程序][SQLServer]'@P11' 附近的语法不正确。
错误发生在第 57 行。

我花了一段时间才弄清楚@P11 是什么,但似乎第一个查询中的 FrequencyUnit 或第二个插入查询中的 RBID 都有问题。

数据类型设置正确。

先感谢您。

编辑:

SQL 字符串 (param 1) = [type='IN', class='java.lang.Integer', value='15488', sqltype='CF_SQL_INTEGER'] , (param 2) = [type='IN', class ='java.lang.Integer', value='21066', sqltype='CF_SQL_INTEGER'] , (param 3) = [type='IN', class='java.sql.Timestamp', value='2013-03 -22 00:00:00.0', sqltype='CF_SQL_TIMESTAMP'] , (param 4) = [type='IN', class='java.lang.Integer', value='143933', sqltype='CF_SQL_INTEGER'] , (param 5) = [type='IN', class='java.lang.String', value='Software Test 3', sqltype='CF_SQL_VARCHAR'] , (param 6) = [type='IN', class='java.lang.Float', value='1.0', sqltype='CF_SQL_REAL'] , (param 7) = [type='IN', class='java.lang.Float',value='33.0', sqltype='CF_SQL_REAL'] , (param 8) = [type='IN', class='java.lang.Boolean', value='false', sqltype='CF_SQL_BIT'] , (param 9) = [type='IN', class='java.lang.String', value='Software Test 3', sqltype='CF_SQL_LONGVARCHAR'] , (param 10) = [type='IN', class=' java.lang.Integer', value='0', sqltype='CF_SQL_INTEGER'] , (param 11) = [type='IN', class='java.lang.Integer', value='153314', sqltype= 'CF_SQL_INTEGER']CF_SQL_LONGVARCHAR'] , (param 10) = [type='IN', class='java.lang.Integer', value='0', sqltype='CF_SQL_INTEGER'] , (param 11) = [type='IN' , 类='java.lang.Integer', 值='153314', sqltype='CF_SQL_INTEGER']CF_SQL_LONGVARCHAR'] , (param 10) = [type='IN', class='java.lang.Integer', value='0', sqltype='CF_SQL_INTEGER'] , (param 11) = [type='IN' , 类='java.lang.Integer', 值='153314', sqltype='CF_SQL_INTEGER']

这是我到目前为止得到的:

<cftransaction>


<cfset NextBillingDate = #DateAdd("#FrequencyUnit#", "#FrequencyDuration#", #DateCompleted#)#>

<cfquery name="recbilling" datasource="#request.dsn#" result="myResult">
INSERT INTO RBilling
(CID, UserID, DateCompleted, ClientID, Title, Qty, Rate, Taxable, Description, NextBillingDate,
FrequencyUnit, FrequencyDuration, ProjectID)
VALUES (
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">, 
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">, 
<cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">, 
<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">, 
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#NextBillingDate#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyUnit#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyDuration#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">)
</cfquery>


<cfoutput>
<Cfset RBID="#myResult.identitycol#">
</cfoutput>





<cfquery name="submithourssubmit" datasource="#request.dsn#">
INSERT INTO Billedfees
(CID, UserID, DateCompleted, ClientID, Title, Qty, Rate, Taxable, Description, ProjectID, RBID)
VALUES (
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">,
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">, 
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">, 
<cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">, 
<cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">, 
<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">, 
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#RBID#">
)
</cfquery>

这给了我同样的错误:执行数据库查询时出错。[Macromedia][SQLServer JDBC 驱动程序][SQLServer]'@P11' 附近的语法不正确。
错误发生在第 59 行。

第 59 行是第二个插入查询中的第 59 行。

如果您需要其他任何东西,请告诉我。

谢谢

4

2 回答 2

6

您已经可以使用此访问该变量

<cfoutput>#myResult.identitycol#</cfoutput>
<Cfset rbid = myResult.identitycol>

由于您使用的是 MSSQL,因此您的结果将具有该.identitycol属性。

我相信如果您使用 mysql、oracle、access,它可能会有所不同(从 cf9 开始),但我认为他们在 cf10 中对其进行了标准化。


更新:

错误的原因是您在RBID字段前缺少逗号。这是您的代码的固定版本:

<cftransaction>

    <cfset NextBillingDate = DateAdd(FrequencyUnit, FrequencyDuration, DateCompleted)>

    <cfquery name="recbilling" datasource="#request.dsn#" result="myResult">
        INSERT INTO RBilling 
        (
          CID
          , UserID
          , DateCompleted
          , ClientID
          , Title
          , Qty
          , Rate
          , Taxable
          , Description
          , NextBillingDate
          , FrequencyUnit
          , FrequencyDuration
          , ProjectID
        )
        VALUES 
        (
          <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">
          , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">
          , <cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">
          , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">
          , <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">
          , <cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">
          , <cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">
          , <cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">
          , <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">
          , <cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#NextBillingDate#">
          , <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyUnit#">
          , <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FrequencyDuration#">
          , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">
       )
    </cfquery>

    <cfset RBID = myResult.identitycol>

    <cfquery name="submithourssubmit" datasource="#request.dsn#">
        INSERT INTO Billedfees (
            CID
            , UserID
            , DateCompleted
            , ClientID
            , Title
            , Qty
            , Rate
            , Taxable
            , Description
            , ProjectID
            , RBID
        )
        VALUES (
            <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">
            , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#UserID#">
            , <cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#DateCompleted#">
            , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ClientID#">
            , <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#text0#">
            , <cfqueryparam cfsqltype="CF_SQL_REAL" value="#Qty#">
            , <cfqueryparam cfsqltype="CF_SQL_REAL" value="#text2#">
            , <cfqueryparam cfsqltype="CF_SQL_BIT" value="#Taxable#">
            , <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#text1#">
            , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ProjectID#">
            , <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#RBID#">
        )
    </cfquery>

</cftransaction>
于 2013-03-22T17:40:22.927 回答
4

scope_identity正如我在评论中提到的,除非您运行的是 CF7 或更早版本,否则您不需要使用。从 CF8 开始,新的身份值将放在您的result属性中,即“myResult”。因此,新 ID 在变量中可用:myResult.IDENTITYCOL。

顺便说一句,如果您要使用,scope_identity必须确保在同一个数据库连接上调用它。所以要么在插入查询的末尾调用它:

    <cfquery name="addRecord" ...>
          SET NOCOUNT ON;
          INSERT INTO TableName (....)
          VALUES ( ..... );
          SELECT SCOPE_IDENTITY() AS RBID;
          SET NOCOUNT OFF;
    </cfquery>

    <cfoutput> New RBID: #addRecord.RBID#</cfoutput>

... -=or=- 将两个 cfquery 包装在 acftransaction中以确保保持相同的连接。IE

  <cftransaction>
    <cfquery name="addRecord" ...>
          INSERT INTO TableName (....)
          VALUES ( ..... );
    </cfquery>
    <cfquery name="getNewRecord" ...>
          SELECT SCOPE_IDENTITY() AS RBID;
    </cfquery>

 </cftransaction>
 <cfoutput> New RBID: #getNewRecord.RBID#</cfoutput>
于 2013-03-22T17:57:47.170 回答