devxlogo

Select

Select

Question:
I have a table with three columns. The first, an integer type, is the primary key that is automatically increased by one every time a new row is inserted. What I want to do is insert a row, then somehow get its newly created ID. Is it possible? (I would like to do this without using the cursor.)

Answer:
Whenever a row is inserted to a column with an identity field, the system variable @@IDENTIY is set to the value of the identity field. After the insert just execute a select @@IDENTITY.

See also  5 Ways to Improve Customer Experience
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist