获取我刚刚插入的记录的主键

编程入门 行业动态 更新时间:2024-10-15 18:25:25
本文介绍了获取我刚刚插入的记录的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这变得有些棘手。我想输出刚插入的记录的主键。我正在插入多个记录:

This is becoming kind of tricky. I want to output the primary key of the record I just inserted. I am doing an insert of multiple records:

我的插入语句如下所示:

My insert statement looks something like this:

<cfquery name="imprtFiles" datasource="#mydsn#" result="#result#"> <cfoutput query="myFileList"> INSERT INTO myTablename (mycolumn) VALUES ('#valuegohere#'); </cfoutput> </cfquery> <cfset newID = result.IdentityCol> <cfoutput>#newID#</cfoutput>

这会引发CF错误:

元素IDENTITYCOL在结果中未定义。

"Element IDENTITYCOL is undefined in RESULT."

因此,我认为希望有另一个获取我刚刚插入的记录的PK的方法。有什么想法吗?

So I'm thinking that there is hopefully another way to get PK of the record I just inserted. Any thoughts?

这是我根据示例使用的代码:

Here is the code I used according to the example:

<cftransaction> <cfquery name="importFiles" datasource="#dsn#" result="result"> <cfoutput query="myFileList"> INSERT INTO tbl_logfiles (originalFile, originalFileSize) VALUES ('#name#', '#length#'); </cfoutput> </cfquery> <cfquery name="getID" datasource="#dsn#"> select Max(fileID) as NewID from tbl_logfiles; </cfquery> </cftransaction> <cfset newID = getID.NewID> <cfoutput> #newID# </cfoutput>

我从 #newID#获得的输出是280,这是我数据库表中当前最高的fileID。

The output I get from #newID# is 280, which is the highest fileID in my database table at this time. Weird.

我要获取的是我导入的最后任何 N 条记录。我希望有一种方法可以基于 cfquery.result 从 cfoutput 标记以某种方式输出它。 / p>

What I am trying to get is the last whatever N records I imported. I was hoping there was a way I could output it somehow from the cfoutput tag based on the cfquery.result.

推荐答案

在Coldfusion中有多种实现方法-在此处查看: tutorial480.easycfm/

There are various ways to do it in coldfusion - check it out here: tutorial480.easycfm/

编辑:

尝试此方法,它将最终生效:

Try this method, it will definitly work:

<cftransaction> <cfquery name="imprtFiles" datasource="#mydsn#" result="#result#"> <cfoutput query="myFileList"> INSERT INTO myTablename (mycolumn) VALUES ('#valuegohere#'); </cfoutput> </cfquery> <cfquery name="getID" datasource="#mydsn#" result="#result#"> select Max(id) as NewID from myTablename; </cfquery> </cftransaction> <cfset newID = getID.NewID> <cfoutput> #newID# </cfoutput>

根据评论进行编辑:

是, 当然。您在cfquery标记内有一个cfoutput查询,因此您要逐行插入,并且每次都在创建新的ID。如果需要每个ID,则需要将整个内容包装到cfoutput中:

Yes, for sure. You have a cfoutput query within the cfquery tag, so you are inserting row after row, and you are creating a new ID everytime. If you need the IDs for each one, then you need to wrap the whole thing in your cfoutput:

<cftransaction> <cfoutput query="myFileList"> <cfquery name="imprtFiles" datasource="#mydsn#" result="#result#"> INSERT INTO myTablename (mycolumn) VALUES ('#valuegohere#'); </cfquery> <cfquery name="getID" datasource="#mydsn#" result="#result#"> select Max(id) as NewID from myTablename; </cfquery> <cfset newID = getID.NewID> #newID# </cfoutput> </cftransaction>

更多推荐

获取我刚刚插入的记录的主键

本文发布于:2023-10-23 05:18:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   我刚刚

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!