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 9202125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:10:55+00:00 2026-06-17T23:10:55+00:00

I would like to have a stored procedure which inserts rows into a table

  • 0

I would like to have a stored procedure which inserts rows into a table (retrieved from a select query from another table) and for each newly inserted row gets its identity and updates the original table with the identity

Pseudo code-

records = select id,city,state,country from USER where name=@name

for each record in records  // for each rows selected
   insert into LOCATION(city,state,country) values(@record.city,@record.state,@record.country); //inserts a value into LOCATION table
   @id = SCOPE_IDENTITY(); // gets the identity of the newly inserted row
   update USER set LocationId=@id where Id=@record.id //updates the new id back to old table's column
end

This is a data migration task, where we want to segregate the LOCATION from USER table

Thanks in advance for your time and effort for this thread.

  • 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-06-17T23:10:56+00:00Added an answer on June 17, 2026 at 11:10 pm

    You could do something like this:

    DECLARE @InsertedValues TABLE (ID INT, City VARCHAR(50), State VARCHAR(50), Country VARCHAR(50))
    
    INSERT INTO dbo.Location(City, State, Country)
      OUTPUT Inserted.ID, Inserted.City, Inserted.State, Inserted.Country INTO @InsertedValues(ID, City, State, Country)
        SELECT City, State, Country
        FROM dbo.YourSourceTable
    

    With this, you now have the inserted values – including the newly defined identity values – in your @InsertedValues table variable and you can now update the source table as you see fit.

    UPDATE dbo.YourSourceTable
    SET 
       Col1 = iv.Col1,
       Col2 = iv.Col2,  -- etc. - do whatever you nee to do here!
    FROM @InsertedValues iv
    WHERE  .........  -- here, you need some condition to link the inserted values to the original table
    

    This doesn’t require any cursor or any other messy RBAR (row-by-agonizing-row) processing at all – everything is nicely set-based and as fast as it can possibly be.

    Learn more about the OUTPUT clause at MSDN SQL Server Books Online – you can use the OUTPUT clause on insert, update and even delete statements, too!

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

Sidebar

Related Questions

I have a stored procedure which looks like this: BEGIN INSERT INTO result_table (SELECT
I have a stored procedure that inserts batches of millions of rows, emerging from
I have a stored procedure that inserts into a table (where there is an
I would like to have a stored procedure that return 3 results. these results
I have a store with items retrieved from server. I would like to detect
I would like to build a stored procedure which can return the value 0
I'm building a quick csv from a mysql table with a query like: select
I have a function (stored procedure) defined in a database that I would like
HI, I would like to have the configuration data for my application stored in
I have several images stored in content://media/external/images/media. I would like to display by using

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.