Retrieving ID of Last Inserted Table

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

  1. #1

    Default Retrieving ID of Last Inserted Table

    Hi, I've done a search of the forums and although I've found similar questions
    none of them seemed to answer my question.

    I'm currently writing an application for my business which will create
    invoices for me. I have set up 2 MsSql tables, one called tbl_invoices and
    another called tbl_invoice_items. The idea is that I create an invoice in
    tbl_invoices and then start adding line items to it on the next page. The
    invoice_ID field is auto generated by MsSq. Is there a way of retreiving the id
    assigned by the database so I can write the correct invoice_ID to the
    tbl_invoice_items table. Or am I approaching this from the wrong direction.

    I envisaged the flow of the application like so.

    1)Enter Data for Invoice in form eg customer name, date etc
    2)Form Data written to database
    3)Forwards to page with form fields for entering line items
    4)Write invoice items to database and return to 3 until complete.

    I appreciate your help.

    Jim

    JimCu Guest

  2. Similar Questions and Discussions

    1. Ordered Data Inserted into TEMP table, is Read out in Different ORDER??
      Im using a query with OrderBy clause to populate a temp table. The Query works and Order and everything is Fine But when reading the data out of...
    2. Horizontal Scroll appears after 2nd Table inserted
      Hello there, I'm new to Dreamweaver, yet alone programming HTML, so please bare with me... I'm designing a website for a 1024 x 768...
    3. retrieving last record inserted using @@IDENTITY - ASPJScript
      Im triyng to retrieve the last record inserted usnig the "Insert" Server Behavior, in a table that have an auto-incrementig id column. Im trying...
    4. MSACCESS hangs when retrieving LIST type field (linked informix table)
      I have fields like: ,field LIST(VARCHAR(255) NOT NULL) NOT NULL MSACCESS hangs when i retrieve this via a linked table. I also have fields...
    5. Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table.
      We are getting this error after clearing the web.config of database infomation - even after using the wizard to re-enter the information. I could...
  3. #2

    Default Re: Retrieving ID of Last Inserted Record

    Hi,

    Do not no if this is something you can use:

    <cfquery name="id" datasource="">
    SELECT LAST_INSERT_ID() AS highest_id
    </cfquery>

    <cfset vardev_id = id.highest_id>
    <cflocation url="page_2.cfm?invoice_ID=#vardev_id#">

    then on page two make a query (WHERE invoice_ID = #URL.invoice_ID# or
    something) And insert the values you want to insert into the new table.

    Hope this was usefull

    Bye, Kabbi





    kabbi~thkek Guest

  4. #3

    Default Re: Retrieving ID of Last Inserted Record

    wrap two queries inside cftransaction and cflock tags.
    Query 1 - insert your record
    Query 2 - select the max(id) and have a great big where clause.
    Dan Bracuk Guest

  5. #4

    Default Re: Retrieving ID of Last Inserted Record

    Thanks, I'll give them both a go and see how it goes. Should work okay for a
    limited number of users, it's shame that an insert query doesn't return the
    primary key as a value. Perhaps we'll see that in 7.1?

    JimCu Guest

  6. #5

    Default Re: Retrieving ID of Last Inserted Table

    JimCu wrote:
    > I'm currently writing an application for my business which will create
    > invoices for me. I have set up 2 MsSql tables, one called tbl_invoices and
    well for starters don't use anything that uses a select MAX(), that will
    eventually become a bottleneck. if by "MsSql" you mean ms sql server & by "auto
    generated" you mean a column w/an IDENTITY characteristic, then use the native
    sql server stuff in you're cfquery:

    SET NOCOUNT ON
    INSERT INVOICE(....
    SELECT SCOPE_IDENTITY() AS invoiceID
    SET NOCOUNT OFF

    if you're sure there are no triggers that act on tables w/IDENTITY columns then
    you could also use SELECT invoiceID=@@IDENTITY instead of the SCOPE_IDENTITY()
    function. in either case the cfquery resultset will contain an "invoiceID" column.
    PaulH *TMM* Guest

  7. #6

    Default Re: Retrieving ID of Last Inserted Table

    What Paul said - Make sure that the record you get back when going for the
    maxID is the same one you wrote. If you have alot of transactions, you might
    get the wrong one. Using some method other than MaxID is better.

    ptoretti 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