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

Is self-hosting message brokers like RabbitMQ worth it?

I'm curious about the realities of self-hosting message brokers, specifically RabbitMQ. I've been digging into the documentation and it seems quite complex, requiring a...

What’s the Best 4K Monitor for Coding Within a $350 Budget?

I'm a full-time coder working from 9 to 5, and I'm currently looking to upgrade my setup with a new 4K monitor that fits...

Trouble Mapping Network Drives With WHfB: Need Help!

Hey fellow sysadmins! I'm currently running a test deployment of Windows Hello for Business (WHfB) for several users. The deployment was executed through Account...

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

Scavenger Hunt Team Randomizer

Planning a scavenger hunt and need to split participants into random teams? Whether you're organizing a school activity, a corporate team-building event, or a...

File Hash Generator Online – Get Instant MD5 and SHA-256 Hashes

Whether you are validating downloads, checking for corruption, or comparing files for duplicates, having a fast and secure way to generate file hashes is...

Visual CSS Editor for Modern Glass UI Effects

Modern UI design is all about clean, layered aesthetics, and few styles deliver this better than glassmorphism. If you're designing sleek user interfaces and...

Fast and Accurate Tap BPM Counter – Free Web Tool

Whether you're producing music, DJing live, or just figuring out the tempo of a song, knowing the BPM (beats per minute) can be critical....

Glassmorphism CSS Generator with Live Preview

Glassmorphism is one of the most visually striking design trends in modern UI. Its soft, frosted-glass effect adds depth and elegance to web interfaces,...

Add Custom Speech and Caption Boxes to Any Image Online

Creating comic-style images used to require complex design tools or specialist software. Whether you're making memes, teaching graphics, social media posts or lighthearted content,...

Latest Posts

Latest Questions