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

  • SEARCH
  • Home
  • 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 82227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:35:02+00:00 2026-05-10T21:35:02+00:00

I am currently working in C#, and I need to insert a new record

  • 0

I am currently working in C#, and I need to insert a new record into one table, get the new primary key value, and then use that as a foreign key reference in inserting several more records. The Database is MS SQL Server 2003. All help is appreciated!

  • 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. 2026-05-10T21:35:03+00:00Added an answer on May 10, 2026 at 9:35 pm

    The way to get the identity of the inserted row is with the SCOPE_IDENTITY() function. If you’re using stored procedures then this would look something like the following to return the row identity as an output parameter.

    CREATE PROCEDURE dbo.MyProcedure (     @RowId INT = NULL OUTPUT ) AS  INSERT INTO MyTable (     Column1     ,Column2     ,... ) VALUES (     @Param1     ,@Param2     ,... );  SET @RowId = SCOPE_IDENTITY(); 

    You can then use this value for any subsequent inserts (alternatively, if you can pass the data all into the stored procedure, then you can use it in the remainder of the procedure body).

    If you’re passing the SQL in dynamically then you use much the same technique, but with a single string with statement delimiters (also ; in SQL), e.g.:

    var sql = 'INSERT INTO MyTable (Column1, Column2, ...) VALUES (@P1, @P2, ...);' +           'SELECT SCOPE_IDENTITY();'; 

    Then if you execute this using ExecuteScalar you’ll be able to get the identity back as the scalar result and cast it to the right type. Alternatively you could build up the whole batch in one go, e.g.

    var sql = 'DECLARE @RowId INT;' +            'INSERT INTO MyTable (Column1, Column2, ...) VALUES (@P1, @P2, ...);' +           'SET @RowId = SCOPE_IDENTITY();' +           'INSERT INTO MyOtherTable (Column1, ...) VALUES (@P3, @P4, ...);'; 

    This may not be exactly the right syntax, and you may need to use SET NOCOUNT ON; at the start (my mind is rusty as I rarely use dynamic SQL) but it should get you on the right track.

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

Sidebar

Related Questions

I am currently working on an app, where I need to insert contacts into
I just need to be able to insert a key/value pair into an object
I am currently working with a file name and I need to get the
I have a MVC 4 project I am currently working on and need some
I am currently working on a project where I need to implement a parallel
I'm currently working on a NuGet package which need to configure a couple of
I am currently working on a project where I need to generate multiple values
I am currently working in an application where I need to find all occurrences
I am currently working on a grails project where I need to execute a
I'm currently working with a big container that i need to iterater over an

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.