This is my table.
Ticket(id int auto_increment,name varchar(50));
after inserting into this table i want send id and name to mail.. how can i get the last Inserted value.
help to solve this...
Use @@IDENTITY to Return the Last-Inserted Identity Value in SQL Server. In SQL Server, you can use the T-SQL @@IDENTITY system function to return the last-inserted identity value in the current session. Note that it returns the last identity value generated in any table in the current session.
The following is the syntax to get the last 10 records from the table. Here, we have used LIMIT clause. SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query.
USE GEEKSFORGEEKS; Output: To have the latest updated records, we should have a column such as “last updated” with the “Timestamp” column in any table and when a record is newly inserted, it should get the current timestamp value for that column. This will help during record creation.
IDENT_CURRENT() will give you the last identity value inserted into a specific table from any scope, by any user.
Look into: Scope_identity
SCOPE_IDENTITY()
The table which is having Identity Property from that table we can get the last inserted record id will be like this
SELECT SCOPE_IDENTITY()
OR
But this is not a safe technique:
SELECT MAX(Id) FROM Ticket
OR
This is also not a safe technique:
SELECT TOP 1 Id FROM Ticket ORDER BY Id DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With