Get Last ID from a Table
-
hi to all, I want to get last inserted id from a particular table, how i write the sql query.....
-
hi to all, I want to get last inserted id from a particular table, how i write the sql query.....
Normally, you wouldn't do this at all. You let the database keep track of and assign the ID itself when you create a record. Why? Well, say you have 2 clients accessing the database at the same time. They both execute that little function that returns the last ID used and they both get the same ID number. Then, they both generate the next logical ID number and try to use it. Boom. You either just corrupted your data because you don't have any constraints on the key column or you just got an exception thrown back in your face about using the same key more than one in a table.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
hi to all, I want to get last inserted id from a particular table, how i write the sql query.....
-
Normally, you wouldn't do this at all. You let the database keep track of and assign the ID itself when you create a record. Why? Well, say you have 2 clients accessing the database at the same time. They both execute that little function that returns the last ID used and they both get the same ID number. Then, they both generate the next logical ID number and try to use it. Boom. You either just corrupted your data because you don't have any constraints on the key column or you just got an exception thrown back in your face about using the same key more than one in a table.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Although you have 3 remarkable haphazard in your example, but they are acceptable :) Every table should have unique key (PK), that key is usually an Autoinc field type, when insert new record, the table will automatically generate the next ID to be used for the next client. But very often, you need to make your own sequence number need to be written in the table, and you can usually do that by creating a table (numbering) which store in it the next ID for (let say new invoice) and you can luck this record and prevent any one from accessing this record and the user get his ID and unlock the row. My point is, you will not let the database generate new ID for you in every situation.
Samir R. Ibrahim
modified on Friday, February 13, 2009 7:50 AM
-
Although you have 3 remarkable haphazard in your example, but they are acceptable :) Every table should have unique key (PK), that key is usually an Autoinc field type, when insert new record, the table will automatically generate the next ID to be used for the next client. But very often, you need to make your own sequence number need to be written in the table, and you can usually do that by creating a table (numbering) which store in it the next ID for (let say new invoice) and you can luck this record and prevent any one from accessing this record and the user get his ID and unlock the row. My point is, you will not let the database generate new ID for you in every situation.
Samir R. Ibrahim
modified on Friday, February 13, 2009 7:50 AM
True, but locking records manually is VERY problematic if not done with the utmost of care. Most newbies asking questions around here have no idea what they're doing, so using a solution like this is going to be beyond them.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
True, but locking records manually is VERY problematic if not done with the utmost of care. Most newbies asking questions around here have no idea what they're doing, so using a solution like this is going to be beyond them.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008I understood what you want to say. Regards.
Samir R. Ibrahim