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

发布于 2024-11-03 08:58:24 字数 1270 浏览 1 评论 0 原文

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

我的插入语句看起来像这样:

<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 在 RESULT 中未定义。”

所以我想希望有另一种方法来获得我刚刚插入的记录的 PK。有什么想法吗?

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

<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。诡异的。

我想要获取的是我导入的最后一个 N 记录。我希望有一种方法可以根据 cfquery.resultcfoutput 标签输出它。

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>

And this throws a CF Error:

"Element IDENTITYCOL is undefined in RESULT."

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>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尝蛊 2024-11-10 08:58:24

我知道这是一个旧线程,但是......永远不要使用 SELECT MAX(Column) 来获取最后插入的 ID。 它即使在事务中(设置为默认级别),也不是线程安全的

由于 result 属性仅返回单个标识值,因此您必须使用单独的查询(无论如何对于 SQL Server 2000)。然后,您可以在每次插入后获取并存储新的 ID。

另外,请务必对所有参数使用 cfqueryparam。除了防止 SQL 注入之外,它还会通过鼓励数据库 insert 查询的性能-affect-performance-for-constants-and-null-values/17582859#17582859">重用相同的查询计划

<cfset newIDArray = []>
<cftransaction>
    <cfloop query="myFileList">
        <cfquery result="result" ...>
           INSERT INTO myTablename (...) VALUES (...);
        </cfquery>
        <!--- For CF9+ use generic result.generatedKey instead --->
        <cfset arrayAppend(newIDArray, result.identityCol)>
    </cfloop>
</cftransaction>

注意:正如 Nicholas 在评论中提到的,CF9+ 引入了一个新的与数据库无关的键名 result.generateKey。因此,如果您使用的是更高版本,则该键名称优先于 result.identityCol 以获得更大的灵活性。

显然,有更好的选择,例如 SQL Server 2005 的OUTPUT

I know this is an old thread, but ... never use SELECT MAX(Column) to obtain the last ID inserted. It is not thread safe, even within a transaction (set at the default level).

Since the result attribute only returns a single identity value, you must use separate queries instead (for SQL Server 2000 anyway). Then you can grab and store the new id after each insert.

Also, be sure to use cfqueryparam on all parameters. In addition to protecting against sql injection, it will boost performance of the insert queries by encouraging the database to reuse the same query plan.

<cfset newIDArray = []>
<cftransaction>
    <cfloop query="myFileList">
        <cfquery result="result" ...>
           INSERT INTO myTablename (...) VALUES (...);
        </cfquery>
        <!--- For CF9+ use generic result.generatedKey instead --->
        <cfset arrayAppend(newIDArray, result.identityCol)>
    </cfloop>
</cftransaction>

Note: As Nicholas mentioned in the comments, CF9+ introduced a new database agnostic key name result.generatedKey. So if you are using a later version, that key name is preferred over result.identityCol for greater flexibility.

Obviously there are better options like OUTPUT for SQL Server 2005.

杀手六號 2024-11-10 08:58:24

它将是 MSSQL 2000 或 2005。如果您使用 2005,则可以在 INSERT 查询中使用 OUTPUT 子句来跟踪插入创建的 PK 值。

有关输出

要将其放入您的解决方案中,您可能必须将其设置为存储过程并从 CF 调用 SP。

It would be MSSQL 2000 or 2005. If you're using 2005, you can use the OUTPUT clause in your INSERT query to keep track of the PK values created by the insert.

See this BOL article for more on OUTPUT.

To put this in your solution, you may have to set it up as a stored procedure and call the SP from CF.

好菇凉咱不稀罕他 2024-11-10 08:58:24

在 Coldfusion 中有多种方法可以做到这一点 - 请在此处查看:http://tutorial480.easycfm.com/

编辑:

尝试这个方法,它肯定会起作用:


<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 中:


<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>

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

EDIT:

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>

EDIT based on comment:

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>
玻璃人 2024-11-10 08:58:24

我认为在这种情况下,您需要在自己的 cfquery 标记中执行每个插入。如果您一次执行一项插入,那不会有问题,但是因为您正在执行多次插入(并且您使用的是较旧版本的 SQL Server),所以无法使用单个 < 来完成此操作代码>cfquery。如果您使用的是 2005 年或 2008 年,您可能会使用另一个回复中提到的 OUTPUT 方法。

I think in this case you'd need to do each of the inserts in their own cfquery tag. If you were doing one insert at a time, that wouldn't be a problem, but because you're doing multiple inserts (and you're on an older version of SQL Server), there's no way to do this with a single cfquery. If you're on 2005 or 2008 you could potentially use the OUTPUT method mentioned in another reply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文