getting the last ID in db

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

  1. #1

    Default getting the last ID in db

    Hi

    I have been using the following code to get the last id in my database
    tables:

    <cfquery name = "getid" datasource = "#SESSION.clientdatasource#">
    SELECT @@IDENTITY FROM tbl_templates as lastadded
    </cfquery>

    This works fine if there are records, but if the recordcount is 0 i do not
    get a value, is there another way to do this? I am using MySQL.

    Cheers in advance!

    Shaun


    Shaun Perry Guest

  2. #2

    Default Re: getting the last ID in db

    It could be as a result the peculiarity of @@IDENTITY, that
    it applies to all your tables, not just to one, and to every scope
    of each table. Therefore, the result you obtain could have been
    caused, for example, by some trigger-scope change
    in another table. I should experiment with the following function
    instead

    SELECT IDENT_CURRENT('tbl_templates')



    BKBK Guest

  3. #3

    Default Re: getting the last ID in db

    Assuming you are talking about MS SQL (SQL Server) not mySQL ....

    Are you trying to retrieve a new record ID after an INSERT statement? If so,
    are you wrapping both the INSERT and SELECT @@IDENTITY inside a CFTRANSACTION?

    If you just want to retrieve the last ID in your tables, you probably want

    - SELECT MAX(IDColumn) FROM yourTable

    and not

    - SELECT @@IDENTITY

    Maybe I am misunderstanding you, but in most cases you would not use
    @@IDENTITY to retrieve the last ID

    mxstu Guest

  4. #4

    Default Re: getting the last ID in db

    Yes, I use SELECT MAX(ID column) FROM table all the time. If max ID is 0, then that's what I get.
    The Law Guest

  5. #5

    Default Re: getting the last ID in db

    This is gonna sound lame b/c i have no proof of this b/c it's been about a
    year, but....

    i set up a simultaneous user test using apache JMeter and had 10 users every
    10 seconds hit a CF page. One of the queries used @@Identity, the other used
    Ident_current(). I thought that ident_current() was what i needed, but i
    wanted to verify it.

    I don't remember all the details, but I do remember that ident_current was
    definitely not what I needed, and that @@Identity worked correctly.

    you could do something like select (Case when isnull(@@identity) then 0 else
    @@identity end) as ID (syntax is probably wrong, but that's the Idea). that
    might get you what you need.



    "BKBK" <webforumsuser@macromedia.com> wrote in message
    news:dc0r0r$isc$1@forums.macromedia.com...
    > It could be as a result the peculiarity of @@IDENTITY, that
    > it applies to all your tables, not just to one, and to every scope
    > of each table. Therefore, the result you obtain could have been
    > caused, for example, by some trigger-scope change
    > in another table. I should experiment with the following function
    > instead
    >
    > SELECT IDENT_CURRENT('tbl_templates')
    >
    >
    >

    Marc E 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