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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:20:34+00:00 2026-06-03T00:20:34+00:00

Is This Possible? Here is something I’m looking for, executed together: First, it would

  • 0

Is This Possible?

Here is something I’m looking for, executed together:

First, it would execute the INSERT based on how many rows in the SELECT

  INSERT INTO TABLE2 (xID, NAME)
  SELECT xID, NAME FROM TABLE

Then getting the @@IDENTITY of each INSERTED ROW, it would create a new Insert including the same data of the first SELECT statement:

  INSERT INTO TABLE3 (xID, NAME, ID)
  SELECT xID, NAME, ID as Scope_IdentitY()

If not, what the best way without using cursor or while?

  • 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-03T00:20:36+00:00Added an answer on June 3, 2026 at 12:20 am

    You have, at least, two options:

    1) The OUTPUT…INTO target_table clause (SQL2005+)

    2) Or you could write composable DML(SQL2008+).

    Example:

    DECLARE @Table2 TABLE(
        ID  INT IDENTITY PRIMARY KEY, --IDENTITY 
        xID INT NOT NULL,
        NAME VARCHAR(25) NOT NULL
    );
    
    DECLARE @Table3 TABLE(
        ID  INT PRIMARY KEY, --No IDENTITY 
        xID INT NOT NULL,
        NAME VARCHAR(25) NOT NULL
    );
    
    --First solution: OUTPUT ... INTO
    INSERT  INTO @Table2 (xID, NAME)
    OUTPUT  inserted.xID, inserted.NAME, inserted.ID INTO @Table3(xID, NAME, ID)
    SELECT  t.Col1, t.Col2
    FROM    (SELECT 11,'A' UNION ALL SELECT 22,'B' UNION ALL SELECT 33,'C') AS t(Col1,Col2);
    
    --Second solution: composable DML
    INSERT  INTO @Table3(xID, NAME, ID)
    SELECT  src.xID, src.NAME, src.ID
    FROM
    (
            INSERT  INTO @Table2 (xID, NAME)
            OUTPUT  inserted.xID, inserted.NAME, inserted.ID 
            SELECT  t.Col1, t.Col2
            FROM    (VALUES(44,'D'),(55,'E'),(66,'F')) AS t(Col1,Col2)
    ) src
    
    SELECT * FROM @Table2 
    SELECT * FROM @Table3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if this is possible but here is what I would like to
Is this possible to do the following in ExpressionEngine: (code taken from here )
Is this possible? I've followed the instructions from here ( http://www.jetbrains.net/confluence/display/TCD4/Setting+up+an+External+Database#SettingupanExternalDatabase-MicrosoftSQLServer2005 ) but I
I doubt this is possible, but here's what I want to do. If my
I do not know if this is possible, but here goes. And working with
I'm guessing this isn't possible, but here goes. I have two tables, and I'm
Just a follow up question to this one here => link Is it possible
Is this possible? I am looking forward to a tutorial which explains the steps
Is this possible? Code: $('#goRightBtn').click(function() { Random code here }; I have a button,
I doubt this is possible but here goes. I need to formulate a SQL

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.