在CFScript中获取新插入的记录ID [英] Get newly inserted record ID in CFScript

查看:92
本文介绍了在CFScript中获取新插入的记录ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以将记录和请求信息一起插入到日志中.发送请求并发送回响应后,我将使用响应信息更新记录. 有没有办法获取新插入记录的ID,以便我可以引用该ID并在收到响应后对其进行更新? 我知道使用CF标签可以使用SET NO COUNT,但是在CFScript中似乎不起作用. 似乎在INSERT语句中未返回任何内容.

I have some code that inserts a record into the log along with the request information. Once the request is sent and a response is sent back I update the record with the response info. Is there a way to get the ID of the newly inserted record so I can reference that and update it once I receive the response? I know using CF tags you can use SET NO COUNT but it doesn't seem to work in CFScript. Seems like nothing is returned in an INSERT statement.

        query = new query();
        query.name = "queryResult";
        query.setDataSource('dsn');
        sql = "

        INSERT INTO paymentlogs (
            type, name, address1, address2, country, provstate, city, pocode, cclastfour, expirymonth, expiryyear, cardtype, requestxml, ipaddress, clid, subscriptionid, total, createdat, createdby, updatedat, updatedby
        )
        VALUES
        (
            :type, :name, :address1, :address2, :country, :provstate, :city, :pocode, :cclastfour, :expirymonth, :expiryyear, :cardtype, :requestxml, :ipaddress, :clid, :subscriptionid, :total, :now, :username, :now, :username
        )

        ";
        query.setSQL(sql);

        query.addParam(name="clid", cfsqltype="cf_sql_varchar", value="#formValues.clid#");
        query.addParam(name="type", cfsqltype="cf_sql_varchar", value="#arguments.type#");
        query.addParam(name="name", cfsqltype="cf_sql_varchar", value="#formValues.ccname#");
        query.addParam(name="address1", cfsqltype="cf_sql_varchar", value="#formValues.ccaddress1#");
        query.addParam(name="address2", cfsqltype="cf_sql_varchar", value="#formValues.ccaddress2#");
        query.addParam(name="country", cfsqltype="cf_sql_varchar", value="#formValues.cccountry#");
        query.addParam(name="provstate", cfsqltype="cf_sql_varchar", value="#formValues.ccprovstate#");
        query.addParam(name="city", cfsqltype="cf_sql_varchar", value="#formValues.cccity#");
        query.addParam(name="pocode", cfsqltype="cf_sql_varchar", value="#formValues.ccpocode#");
        query.addParam(name="cclastfour", cfsqltype="cf_sql_varchar", value="#Right(formValues.ccnumber, 4)#");
        query.addParam(name="expirymonth", cfsqltype="cf_sql_varchar", value="#formValues.ccexpirymonth#");
        query.addParam(name="expiryyear", cfsqltype="cf_sql_varchar", value="#formValues.ccexpiryyear#");
        query.addParam(name="cardtype", cfsqltype="cf_sql_varchar", value="#getCardType(formValues.cctype)#");
        query.addParam(name="requestxml", cfsqltype="cf_sql_varchar", value="#soapBody#");
        query.addParam(name="ipaddress", cfsqltype="cf_sql_varchar", value="#CGI.REMOTE_ADDR#");
        query.addParam(name="subscriptionid", cfsqltype="cf_sql_varchar", value="#formValues.subscriptionid#");
        query.addParam(name="total", cfsqltype="cf_sql_float", value="#formValues.grandTotalAmount#");
        query.addParam(name="username", cfsqltype="cf_sql_varchar", value="#formValues.username#");
        query.addParam(name="now", cfsqltype="cf_sql_timestamp", value="#now()#");
        result = query.execute().getResult();
        writedump(result);abort;

我已经搜索过Google,但找不到任何通过CFScript进行搜索的方法.我不想查询表的最后一行,因为那不是很可靠.执行INSERT查询后,是否可以获取新插入的记录ID?

I've searched Google and couldn't find any way to do it in CFScript. I don't want to have to query the table for the last row because that is not very reliable. Is there a way to get the newly inserted record ID after executing the INSERT query?

我正在使用mySQL.奇怪的是,当我转储上面的结果"变量时,CF抱怨结果是不确定的.当我检查表时,可以看到脚本确实已执行并且记录已插入.

I'm using mySQL. The strange thing is when I dump out the "result" variable above, CF complains that result is undefined. When I check the table, I can see that the script did execute and the record was inserted.

推荐答案

它应位于getPrefix().generatedkey

genKey = result.getPrefix().generatedkey;

这篇关于在CFScript中获取新插入的记录ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆