How to get an ID of the last inserted row of a table
-
I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.
-
I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.
-
This query
select top 1 id from tablename order by id desc
will not be choice for you?
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
yeah, it worked. Thank you very much. Still, I have a question: isn't it the same as choosing the maximum ID?
-
yeah, it worked. Thank you very much. Still, I have a question: isn't it the same as choosing the maximum ID?
makumazan84 wrote:
Thank you very much.
No problem. If ID value is increase-able always then we can use
max
function to get maximum value of ID. example:select max(id) from tablename
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
-
This query
select top 1 id from tablename order by id desc
will not be choice for you?
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
This only works in a single user environment - otherwise someone else could insert before you run the select. Also, this relies on id being the prmary key, otherwise you should select the max(id) - bad practice to rely on any ordering unless specified. Do't know what the answer is though, I nvere use tableadaptors etc, there isn't enough control over transactions etc for the sort of systems I usually work on.
Bob Ashfield Consultants Ltd
-
This only works in a single user environment - otherwise someone else could insert before you run the select. Also, this relies on id being the prmary key, otherwise you should select the max(id) - bad practice to rely on any ordering unless specified. Do't know what the answer is though, I nvere use tableadaptors etc, there isn't enough control over transactions etc for the sort of systems I usually work on.
Bob Ashfield Consultants Ltd
-
I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.
select max(id) from mytable should be enought but...you may need to use a lock. Read about it i don´t know much about it! If it´s a read/report procedure (you don´t delete or edit anything) there´s no problem using select max(id )from mytable, otherwise you must use somekind of lock!!! To prevent users to edit same thing same time X|
nelsonpaixao@yahoo.com.br trying to help & get help