Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

I'm trying to get a the key-value back after an INSERT-statement. Example: I've got a table with the attributes name and id. id is a generated value.

 INSERT INTO table (name) VALUES('bob');

Now I want to get the id back in the same step. How is this done?

We're using Microsoft SQL Server 2008.

1 Answer

0 votes
by (40.4k points)
edited by

You can use the code given below, instead of using separate SELECT.

Are you interested in learning SQL from the basics! Refer to this video on SQL provided by Intellipaat:

Query:

INSERT INTO table (name)

OUTPUT Inserted.ID

VALUES('bob');

The above code only works for Non-IDENTITY columns.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...