Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SERVER 2008 GET LAST INSERTED VALUES

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...

like image 887
Raj Adroit Avatar asked Sep 25 '12 11:09

Raj Adroit


People also ask

How do you get the last inserted identity value?

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.

How can I get the last 10 values of a table in SQL?

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.

How do I find recently added records in SQL?

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.

Is used to retrieve last inserted value?

IDENT_CURRENT() will give you the last identity value inserted into a specific table from any scope, by any user.


1 Answers

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
like image 132
Vishal Suthar Avatar answered Oct 10 '22 20:10

Vishal Suthar