我在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'],(参数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.l ang.Integer',value ='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行是第二个插入查询。
如果您还有其他需要,请告诉我。
由于
答案 0 :(得分: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>
答案 1 :(得分:4)
正如我在评论中提到的,除非您运行的是CF7或更早版本,否则您不需要使用scope_identity
。从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>
... - =或= - 将两个cfquery包装在cftransaction
中以确保您保持相同的连接。即
<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>