在CFScript中获取新插入的记录ID

时间:2014-08-12 15:46:08

标签: mysql coldfusion cfml

我有一些代码会将记录与请求信息一起插入日志中。一旦发送请求并发回响应,我就用响应信息更新记录。 有没有办法获取新插入的记录的ID,以便我可以引用它并在收到响应后更新它? 我知道使用CF标签你可以使用SET NO COUNT但它似乎不适用于CFScript。 似乎在INSERT语句中没有返回任何内容。

        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?

我正在使用mySQL。奇怪的是当我抛弃"结果"如上所述,CF抱怨结果未定义。当我检查表时,我可以看到脚本确实执行并且插入了记录。

3 个答案:

答案 0 :(得分:14)

它应位于getPrefix().generatedkey

genKey = result.getPrefix().generatedkey;

答案 1 :(得分:6)

Matt的回答是正确的,元数据位于Prefix属性中。他的例子假设结果变量是返回。 <{1}}代码中没有result = query.execute();

来自query cfc documentation:&#34;前缀:相当于cfquery标记的结果属性。&#34;

答案 2 :(得分:1)

或者您可以使用Scope_Identity() MS SQL Server功能:

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
    )

    select Scope_Identity() as [ID]

    ";

然后result.ID应包含生成的ID。