Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:46:44+00:00 2026-05-31T15:46:44+00:00

I have a simple table with 2 columns – name and the primary key,

  • 0

I have a simple table with 2 columns – name and the primary key, id. When I insert a value, I wish to be able to retrieve the value straight away in my code.

So I have :

db.Execute("INSERT INTO table(name) VALUES (@0)",name);

The id column is not automatically populated and the row is stored.

So how can I db.Query(); for this value when the name is not unique? Is this possible?

An interesting problem, I think 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:46:45+00:00Added an answer on May 31, 2026 at 3:46 pm

    You can add a part to your insert that returns the identity using @@IDENTITY:

    INSERT INTO table(name) VALUES (@0); SELECT @@IDENTITY;
    

    Update: As noted in comments since @@IDENTITY works globally you should actually use SCOPE_IDENTITY() instead to limit to the current scope:

    INSERT INTO table(name) VALUES (@0); SELECT SCOPE_IDENTITY();
    

    Then you can retrieve the primary key / identity by executing your insert with ExecuteScalar() and grabbing the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have this simple table: Table Name: Table1 Columns: Col1 NUMBER (Primary Key)
I have a simple mysql table with five columns: id: auto_increment (primary) id_symbol: INT(4)
I have a very simple HTML table with 4 columns: Facility Name, Phone #,
I have simple table with two columns: id INTEGER as a key, and data
I have a pretty simple table with 5 columns: key, datetime, title, body, image
Does the JPA specification allow simple references to non-primary key columns? I have a
I have a simple country table using a identity column for the primary key.
I have a simple HTML Table (10 columns x 500 rows). When the page
I have a very simple table with two columns, but has 4.5M rows. CREATE
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.