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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:46:53+00:00 2026-06-07T02:46:53+00:00

I am trying to copy multiple records using one query using insert select from.

  • 0

I am trying to copy multiple records using one query using insert select from.

Insert into tab_A(colId, col1, col2, col3)
Select colId, col1, col2, col3  form tab_A
Where colId in ( 2,4,6)

Would it be possible to assign different colId for new entries? For example colid 2 should be replaced with 23, 4 with 24 and 6 with 25. How could I achieve it in a single query?

  • 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-07T02:46:57+00:00Added an answer on June 7, 2026 at 2:46 am

    this would work

    Insert into tab_A(colId, col1, col2, col3)
    Select 23 , col1, col2, col3  form tab_A Where colId = 2 UNION ALL
    Select 24 , col1, col2, col3  form tab_A Where colId = 4 UNION ALL
    Select 25 , col1, col2, col3  form tab_A Where colId = 6 
    

    If you give some more info I could provide somthing more reusable. Should/is colId (be) an identity column?


    EDIT

    This would work in this very specialised case

    Insert into tab_A(colId, col1, col2, col3)
    Select ((colId - 4) * (-1)) + colId + 20 , col1, col2, col3  
        form tab_A Where colId IN (2, 4, 6)
    

    The function newId = ((oldId - 4) * (-1)) + oldId + 20 is obviously specific to the stated problem.


    EDIT2

    I suspect somthing like this is more generic approach is appropriate.

    DECLARE @MaxColID INT
    
    BEGIN TRANSACTION
    
    SELECT @MaxColID = MAX(ColID) FROM tab_A
    
    INSERT tab_A(colId, col1, col2, col3)
    SELECT row + @MaxColID, col1, col2, col3
        FROM
        (
            SELECT ROW_NUMBER() OVER (ORDER BY ColID) row, col1, col2, col3
            FROM tab_A WHERE colID IN (2, 4, 6)
        )
    
    COMMIT
    

    EDIT 3

    If you think EDIT 2 is actually what you want then you really want to make ColID an IDENTITY column, then you could do this.

    INSERT tab_A (col1, col2, col3)
    SELECT col1, col2, col3  FROM tab_A WHERE colId IN (2, 4, 6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to copy data from table into another by using a SELECT
I'm trying to copy the <options> HTML Codes from one select list to another.
I'm trying to run one SQL command to update multiple databases. If I copy
I'm trying to get data from multiple XML feeds and copy that data to
I'm trying to combine multiple, smaller XML files into one large XML. I have
I'm trying to copy the contents of multiple folders within the same directory into
I'm trying to copy multiple files from a deep source tree that have the
I'm trying to copy a folder recursively to multiple destination folders using MSBuild's Copy
In Visual C#, I'm trying to take text from multiple textboxes (one at a
I'm trying copy a single file from the Plugin directory inside of my Wordpress

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.