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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:19+00:00 2026-05-22T15:28:19+00:00

I have a table Items (ItemID, Name, …) where ItemID is auto-generated identity I

  • 0

I have a table Items (ItemID, Name, …) where ItemID is auto-generated identity

I want to add rows into this table FROM select on this same table.
AND save into table variable the references between OriginalItemID and NewlyGeneratedID.

So I want it to look like the following:

DECLARE @ID2ID TABLE (OldItemID INT, NewItemID INT);

INSERT INTO Items OUTPUT Items.ItemID, INSERTED.ItemID INTO @ID2ID
SELECT * FROM Items WHERE Name = 'Cat';

BUT Items.ItemID obviously does not work here. Is there a workaround to make OUTPUT take original ItemID from the SELECT statement?

  • 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-22T15:28:20+00:00Added an answer on May 22, 2026 at 3:28 pm

    If you are on SQL Server 2008+, you can use MERGE for getting both the current and the new ID. The technique is described in this question.

    For your example the statement might look like this:

    MERGE INTO
      Items AS t
    USING
      (
        SELECT *
        FROM Items
        WHERE Name = 'Cat'
      ) AS s
    ON
      0 = 1
    WHEN NOT MATCHED BY TARGET THEN
      INSERT (target_column_list) VALUES (source_column_list)
    OUTPUT
      S.ItemID, INSERTED.ItemID INTO @ID2ID (OldItemID, NewItemID)
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a field (cat_tags) which contain the following rows:- id
I have two tables Items and ItemDescriptions class Item(Base): __tablename__ = item id =
I have table with check boxes in it.and the CSS for the check box
I have a SQL table with column which contain string like 'type|type1|type2|type3|type4'. I need
I do not know how to classify this question. Vaguely, its about using calculated
I have a dojo layout with two content panes, each at 50% height, one
I have a string comma delimited. I need to select with t-sql just first
I have kind of a tricky Oracle problem. I am trying to select one
I'm new to CakePHP and I trying out some of the tutorials on it.

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.