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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:06:58+00:00 2026-05-15T12:06:58+00:00

I have two table TempStore and Store with the same column called Items. There

  • 0

I have two table “TempStore” and “Store” with the same column called “Items”.

There is data in “TempStore” table which I need to move over to “Store” table which requires few modifications.

I need to iterate over “TempStore” data (i.e. items) and insert into Store…

More specifically: How can I iterate over TempStore (in sql) where “for each item in ‘TempStore’ I need to store 2 or maybe 3 items in ‘Store’ with little modification”, how can I accomplish this?

What I want to do is take each rowdata from “[SELECT * FROM TempStore]” and insert three records in “Store” with being able to change “items”

  • 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-15T12:06:59+00:00Added an answer on May 15, 2026 at 12:06 pm

    try INSERT-SELECT:

    INSERT INTO Store
            (col1, col2, col3...)
        SELECT
            col1, col2, col3...
            FROM TempStore
            WHERE ...
    

    just make the SELECT return one row for every insert, and produce the values in the Cols that you need. You might need CASE and a join to another table to make the extra rows.

    EDIT based on comments, OP wanted to see the numbers table in action

    Lets say TempStore table has {Items,
    Cost, Price, ActualCost, ActualPrice}
    But in the Store table I need to store
    {Items, Cost, Price}. The ActualCost
    and ActualPrice from TempStore datarow
    would need to be added as another row
    in Store….(I hope this makes
    sense)….Anyways, is the solution
    using “WHILE-BEGIN-END”??

    CREATE TABLE Numbers (Number int NOT NULL PRIMARY KEY)
    INSERT INTO Numbers VALUES(1)
    INSERT INTO Numbers VALUES(2)
    INSERT INTO Numbers VALUES(3)
    
    
    INSERT INTO Store
            (Items, Cost, Price)
        SELECT
            t.Items, t.Cost
                ,CASE
                     WHEN n.Number=1 THEN t.Price
                     WHEN n.Number=2 THEN t.ActualCost
                     ELSE t.ActualPrice
                 END
            FROM TempStore         t
                INNER JOIN Numbers N ON n.Number<=3
            WHERE ...
    

    you could even use a UNION:

    INSERT INTO Store
            (Items, Cost, Price)
        SELECT
            t.Items, t.Cost, t.Price
            FROM TempStore t
        UNION ALL
        SELECT
            t.Items, t.Cost, t.ActualCost
            FROM TempStore t
        UNION ALL
        SELECT
            t.Items, t.Cost, t.ActualPrice
            FROM TempStore t
    

    either the Numbers table or the UNION will we WAY better than a loop!

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

Sidebar

Related Questions

I have two table. OLDTABLE and NEWTABLE. I need to insert some data from
Using SQL Server 2005 I have two table which have the same datatype and
i have a two table called A and B both have same structure but
I have two table and I need to copy the data across from SRCServiceUsers
I have two table. One table hold key column and xml column. Other table
I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
I have two table in database A,B A: summary view of my data B:
I have two table Company and Employee. And there relation is Company(one) - Employee(many).
I have two table views displaying the data from a Sqlite database. I can
I have two table and I should write a select query which join this

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.