Get Auto Increment ID From SQL Server Insert

When using Sql Server to insert records into a DB its a common requirement to be able to pull back the auto incremented Id that was generated for this row. Most solutions for this i have seen involve making a second query to the database in and along the lines of  “SELECT @@IDENTITY”, but this method seems quite wasteful. It seems like a simple request that the database would return the value automatically without you having to ask for it with a second wasteful query. Well, it turns out that it’s incredibly simple to do the auto increment ID from SQL Server after an insert is performed.

The auto incremented value can be easily returned by using the OUTPUT Inserted.<auto id col name>. To put this in a real life example, the following query will insert a user into the Users database and will return the auto ID. In this case there is a column called “id” and this is what i am asking the DB to return in the query.

INSERT INTO Users(name, age) OUTPUT Inserted.id VALUES ('Tom', '50');

Using this query from whatever language you use will make the DB return the auto incremented ID from an SQL Server database. Here is an example of how you would use the above query in C# to get the auto incremented ID back from the database.

using(OleDbCommand cmd = new OleDbCommand("INSERT INTO Users(name, age) OUTPUT Inserted.id VALUES ('Tom', '50')"))
{
	cmd.Connection = myCon;
	myCon.Open();
	int scheduleid = (int)cmd.ExecuteScalar(); 
}

 

Related Articles

Related Questions

Am I Ready to Start Freelancing with My Website?

Hey everyone! I'm 26 and originally from Italy, but currently living in France. Right now, I'm stuck in a helpdesk support job, but I...

How to Set Up Jellyfin with a Large Media Library in Docker?

I'm running Jellyfin on my Ubuntu Server 24.xx and I'm considering switching to Docker for better management. However, I have a massive media collection...

How Do I Connect My Power Supply to the Motherboard and GPU?

Hey everyone! I've built my own PC before, but I'm stumped on the PCIe connections this time. My power supply has two connections: one...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions