Two Function Calls to a Database to Retrieve the UniqueID of Inserted Records

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Two Function Calls to a Database to Retrieve the UniqueID of Inserted Records

    Hi guys,

    I got the error message on Option 2 query as show.
    Actually, this code is from Inside ColdFusion MX boook.
    I just would like to know is this code in Option 2 correct?

    <!---
    Error Executing Database Query.
    Missing semicolon (;) at end of SQL statement.
    --->

    Option 1

    <cfquery name="InsertNames" datasource="#request.dsn#">
    INSERT INTO Authors (firstname, lastname, email)
    VALUES ('#form.fname#', '#form.lname#', '#form.email#')
    </cfquery>
    <cfquery name="getNewRecord" datasource="#request.dsn#">
    SELECT FROM Authors
    MAX(AuthorID)
    </cfquery>

    Option 2

    <cfquery name="InsertNames" datasource="#request.dsn#">
    INSERT INTO Authors (firstname, lastname, email)
    VALUES ('#form.fname#', '#form.lname#', '#form.email#')
    SELECT AuthorID
    </cfquery>


    rycbn Guest

  2. Similar Questions and Discussions

    1. retrieve sql from database
      Our organization has alot of SQL, and we prefer to be able to store our application SQL in a DB; we find it easier to maintain that way. What I...
    2. mystery records inserted into database - Please help.
      I recently started migrating from MS Access to MySQL. In order to move some of the information, I used a ColdFusion page with SQL to select...
    3. Retrieve n records from database
      I came across a tutorial about a year ago that explained the answer I seek. Now, however, I cannot find the answer anywhere. I have a flash movie...
    4. How to retrieve the records if I have .SQL script file using ADO.net
      Hi, I need to retrieve records from SQL 2000 datbase using ADO.Net I have a huge select statement in .sql script file I need to run it to get the...
    5. Need to retrieve records
      In the recordset that retrieves the data, add the form variable that the user is inputting as a filter. -- Mike ------------------- Michael...
  3. #2

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    The examples in the book are written for what database type?

    Option 1 looks strange as well. Assuming the queries are wrapped in some type
    of transaction, it should be more like ....



    <cfquery name="InsertNames" datasource="#request.dsn#">
    INSERT INTO Authors (firstname, lastname, email)
    VALUES ('#form.fname#', '#form.lname#', '#form.email#')
    </cfquery>
    <cfquery name="getNewRecord" datasource="#request.dsn#">
    SELECT MAX(AuthorID) AS NewID
    FROM Authors
    </cfquery>

    mxstu Guest

  4. #3

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    As for the second query: Depending on the database, you can run multiple
    queries in one <cfquery> tag. However, SQL statements have to be separated by a
    semi-colon. The semi-colon is the standard SQL "end of statement" delimeter.



    <cfquery name="InsertNames" datasource="#request.dsn#">
    INSERT INTO Authors (firstname, lastname, email)
    VALUES ('#form.fname#', '#form.lname#', '#form.email#')
    ;
    SELECT MAX(AuthorID) AS NewID
    FROM Authors
    </cfquery>

    cf_menace Guest

  5. #4

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    Also, selecting the MAX(ID) to get the recently returned record, WILL break and
    WILL corrupt your results.
    The only question is how long it takes and how long before your boss, or
    customer, discover the problem.

    Do not use that approach on anything you care about.


    MikerRoo Guest

  6. #5

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    If your goal is to get the record ID created by the database, I'd suggest
    adding a date stamp field to the record then querying for the ID by a
    combination of name, email and date to make sure you get the right record.
    Trying to select on the most recent ID will not always work.

    -Paul


    dempster Guest

  7. #6

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    There are better methods than datestamp -- depending on the RDBMS used.
    The only universal approach is a properly designed DB with a unique index on the key values.
    MikerRoo Guest

  8. #7

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    Many thanks for those replied guys. :-)

    I tried to put the semecolon as cf_menace's code above, but I am still getting
    the same error.

    I am using MS Access Database.
    Is MS Access support multiple SQL statements in a single <cfquery>?

    rycbn Guest

  9. #8

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    Is MS Access support multiple SQL statements in a single <cfquery>?

    No.
    You can test this using the Queries->Design->SQL view, in Access itself.

    You have to put these statements into seperate cfqueries. If you use the
    Max(ID) approach, at least surround the Q's with a CFTransaction.

    In this case, it's better to put a unique index on email and then use email in
    your where clause when pulling the inserted record.


    MikerRoo Guest

  10. #9

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    wrapping in a transaction will only work if the isolation level is
    serializable, which basically locks the table for all other queries until these
    2 queries run. if you can, add a new ID column and populate it with a
    createUUID() call, like so:

    <cfset thisID = createUUID()>
    <cfquery blah>
    insert into blah, blah, myIdField
    values(blah,blah,#thisID#)
    </cfquery>
    <cfquery blah>
    select top 1 * from blah where ,myIDField = #thisID# order by
    MyAutoNumberField DESC
    </cfquery>

    the createUUID isn't a 100% guarantee of a unique key, but it's close enough.
    so that, combined with getting the top 1 sorted by your autonumber field in
    descending order guarantees you'll get the last field inserted with that ID,
    which is damn near fool proof unless you're inserting a million records a
    second.

    jonnycattt Guest

  11. #10

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    Adding a UUID column is a waste of time if there is a unique index possible
    (email in this case).

    Also locking the table is not a bad thing. It is required and desirable in
    many situations.
    You can NEVER use max or top autonumber in access since autonumber fields are
    often set to randomize. Even if they are not, you will not get the order you
    expect if you do not use locking.

    Often, all it takes is for the user to double-click the submit button to bust
    these kinds of schemes.

    MikerRoo Guest

  12. #11

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    "Adding a UUID column is a waste of time if there is a unique index possible
    (email in this case)."

    If the application allows for using email as an index, then that's fine. But
    I've never met an app where I'd consider using email.

    "Also locking the table is not a bad thing. It is required and desirable in
    many situations.
    You can NEVER use max or top autonumber in access since autonumber fields are
    often set to randomize. Even if they are not, you will not get the order you
    expect if you do not use locking."

    It's not a bad thing if you have low user loads. But if you do, it most
    certainly is a bad thing. You can use max or top autonumber if you know the
    field is not set to randomize. or, you can have a datetime field and sort by
    date descending that way. the point of the "top 1 * order by XXX desc" is
    merely to get around the small but real possibility that createUUID() will
    return the same value for your app. of course, you could get around this
    completely by making that unique field createUUID() + getTickCount() or + email
    or something. That would ensure uniqueness.

    "Often, all it takes is for the user to double-click the submit button to bust
    these kinds of schemes."

    I'd assume that the application prevents against this. fusebox takes care of
    this naturally.

    jonnycattt Guest

  13. #12

    Default Re: Two Function Calls to a Database to Retrieve theUnique ID of Inserted Records

    Thanks guys.
    I am tried not to use Max(ID) approach for my application.
    rycbn 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