ID of last record inserted

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default ID of last record inserted

    Is it possible, with a MS Access database, to pull the ID of a record just
    inserted? I have found various threads and tutorials on various sites referring
    to commands that can be used for this but they all seem to be unuseable with an
    MS Access database. Many of the sites I have visited have advised there is
    only one way to do this with Access but fail to make it clear how it should be
    done.

    I know that it can done by selecting the highest number ID but on a site that
    may have a lot of traffic and the chance of simultaneous submissions, this
    method would not be very reliable. I can also query the database with all the
    details I have used to create the record in the first place and then output
    the ID value but this seems to be a bit of a dirty way of doing it when there
    are commands that can be used with other databases!

    DJCull Guest

  2. Similar Questions and Discussions

    1. Inserting a timestamp when record is inserted
      Hi, how would I have a form insert a timestamp when the form was submitted for update into a table. I know there is a timestamp column type but I...
    2. Accepted way to grab the ID of a record you have just inserted?
      I have a page that inserts a new record into a DB. Upon doing so, I want to refresh the page and return the data that was just entered (including...
    3. Identity from inserted record
      I'm inserting a record into a SQL Server database. Once the record has been added, I need the Identity value that SQL Server created. Normally, the...
    4. returning an inserted SQL record
      Found my answer... Select max(column) as ID from table
    5. mySQL / Last inserted record ID
      Hi, I'm using mySQL and wanted to get the ID of the record that I have just inserted in to the table (sentmessages). There is a function in mySQL...
  3. #2

    Default Re: ID of last record inserted

    I usally create a
    <Cfquery name = "lastid" dataSource = "#DNS#" maxrow =1 >
    select id
    from database
    order by id desc
    </cfquery>
    <cfoutput query="lastid">#ID# </cfoutput>

    can you post some code. it woudl make it easier to come up with a better answer

    jorgepino Guest

  4. #3

    Default Re: ID of last record inserted

    I have the same question: I am inserting a record into a table, but need the
    autonumber ID to be saved into a related table, using another insert query.

    Here is the code for both tables as current.

    <cfif AddRecord is "yes"><!--- Add Ticket --->
    <CFQUERY NAME="#Table_Name#_Insert_Query" DATASOURCE="HelpDesk">
    INSERT Into #Table_Name# (Entry_Date, Name, Priority_Level,
    Type, Status, Entry_Description, Location)
    VALUES ('#Entry_Date#','#Tech_Name#','#Priority_Level#',
    <cfif #Type# is "">Null
    <cfelse>'#Type#'
    </cfif>, '#Status#', '#Entry_Description#',
    <cfif #Location# is "">Null
    <cfelse>'#Location#'
    </cfif>)
    </CFQUERY>


    <cfquery name = "request" datasource = "helpdesk">
    insert into request (fname, lname, survey, EntryID)
    values ('#fname#', '#lname#', '#survey#', #id#)
    </cfquery>

    Here is what I tried, with no success:
    <cfoutput query = "#Table_Name#_Insert Query">
    <cfset id = #entry_id#>
    </cfoutput>



    Captain Ru Guest

  5. #4

    Default Re: ID of last record inserted

    Here is my solution:

    <cfif AddRecord is "yes"><!--- Add Ticket --->
    <CFQUERY NAME="#Table_Name#_Insert_Query" DATASOURCE="HelpDesk">
    INSERT Into #Table_Name# (Entry_Date, Name, Priority_Level,
    Type, Status, Entry_Description, Location)
    VALUES ('#Entry_Date#','#Tech_Name#','#Priority_Level#',
    <cfif #Type# is "">Null
    <cfelse>'#Type#'
    </cfif>, '#Status#', '#Entry_Description#',
    <cfif #Location# is "">Null
    <cfelse>'#Location#'
    </cfif>)
    </CFQUERY>

    <cfquery name = "lastid" dataSource = "HelpDesk" maxrows = "1">
    select entry_id
    from help_desk_list
    order by entry_id desc
    </cfquery>

    <cfoutput query="lastid">
    <cfset id = #entry_id#>
    </cfoutput>


    <cfquery name = "request" datasource = "helpdesk">
    insert into request (fname, lname, survey, EntryID)
    values ('#fname#', '#lname#', '#survey#', #id#)
    </cfquery>

    Is there a better way?

    Captain Ru Guest

  6. #5

    Default Re: ID of last record inserted

    DJCull,

    For Access, you can use SELECT @@IDENTITY in combination with CFTRANSACTION.
    Grouping the queries within a CFTRANSACTION ensures that you pull ID of the
    record just inserted and that the queries succeed/fail as a single unit.





    <cftransaction>
    <cfquery name="addRecord" ...>
    INSERT INTO yourTable (....)
    VALUES (....)
    </cfquery>
    <cfquery name="getNewRecord" ...>
    SELECT @@IDENTITY AS NewID
    </cfquery>

    <cfquery name="insertRelatedRecord" ...>
    INSERT INTO yourRelatedTable(relatedIDColumn, ...)
    VALUES (#getNewRecord.NewID#, ....)
    </cfquery>
    </cftransaction>

    mxstu Guest

  7. #6

    Default Re: ID of last record inserted

    Captain Ru,

    A few things about the code you posted

    1) At the very least, you should enclose all of the CFQUERY's in a
    CFTRANSACTION to ensure that all of the queries succeed/fail together and that
    the "lastid" query returns the correct record ID.

    2) If you are not using SELECT @@IDENTITY, then the MAX() aggregate

    SELECT MAX(Entry_ID)
    FROM help_desk_list

    ... or even the TOP N operator ...

    SELECT TOP 1 entry_id
    FROM help_desk_list
    ORDER BY entry_id DESC

    would typically provide better performance than the current query :

    select entry_id
    from help_desk_list
    order by entry_id desc

    The CFQUERY "maxrows" attribute only limits the records in the CFQUERY object,
    it doesn't limit the information your database returns, so the current query
    would typically return much more information than is needed. The SELECT MAX()
    method returns only a single record, which is what you want.

    3) You do not need # signs around your variables in the CFIF statements. You
    may also want to TRIM the values to catch invalid values like .... a string of
    5 spaces.

    <cfif Len(Trim(Type)) eq 0>Null<cfelse>'#Type#'</cfif>

    instead of

    <cfif #Type# is "">Null<cfelse>'#Type#'</cfif>

    4) You do not need to use CFOUTPUT when using a query value in a CFSET
    statement. This statement sets the "id" variable to the "entry_id" in the
    first query row.

    <cfset id = lastid.entry_id>

    instead of ...

    <cfoutput query="lastid">
    <cfset id = #entry_id#>
    </cfoutput>

    5. If you are using FORM or URL variables, then you should scope your
    variables in your code, ex. #form.entry_id#


    mxstu Guest

  8. #7

    Default Re: ID of last record inserted

    The transaction and @@ are some things that I haven't yet come across (i'm new)
    but I'll try them out.

    so my...
    <cfquery name = "lastid" dataSource = "HelpDesk" maxrows = "1">
    select top 1 entry_id
    from help_desk_list
    order by entry_id desc
    </cfquery>

    <cfoutput query="lastid">
    <cfset id = #entry_id#>
    </cfoutput>


    would be..

    <cfquery name = "lastid" dataSource = "HelpDesk">
    select @@identity as id
    </cfquery>

    ??


    Captain Ru Guest

  9. #8

    Default Re: ID of last record inserted

    Yes, as long as all of the related queries are within one CFTRANSACTION. You
    should also be using CFQUERYPARAM, for security (see livedocs).

    Also, be careful of sql injection. What type of parameter is #Table_Name# in
    your first insert statement?

    .... INSERT Into #Table_Name# ....

    mxstu Guest

  10. #9

    Default Re: ID of last record inserted

    basically i'm a new hire for a guy that quit, and he started using CF, so now
    I'm learning CF and trying to fix things

    Eventually I'll have all these variables held in the application.cfm.

    Could you suggest any good online tutorials or lessons, or any text? I am
    using the Ben Forta and Nate Weiss 5th Edition Mac CF MX Web App Construct. Kit

    Captain Ru Guest

  11. #10

    Default Re: ID of last record inserted

    Originally posted by: Captain Ru
    Eventually I'll have all these variables held in the application.cfm.

    Could you suggest any good online tutorials or lessons, or any text? I am
    using the Ben Forta and Nate Weiss 5th Edition Mac CF MX Web App Construct. Kit

    I haven't personally read it, but I've heard it provides a pretty good
    introduction to CF MX. As far as web site tutorials, again I haven't reviewed
    any extensively, but [url]http://www.w3schools.com/sql/default.asp[/url]. It has a good
    explanation of most of your basic SQL statements.

    If you are using MX, as you get more experience, you may want to look into
    CFC's . They really help consolidate and compartmentalize your code.



    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139