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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:24:51+00:00 2026-06-07T07:24:51+00:00

I want to perform a big insert statement on multiple lines, but recursion is

  • 0

I want to perform a big insert statement on multiple lines, but recursion is making it hard to build the correct SQL statement. I believe an example will make it easier to explain. Consider the model:

car


|id|code|Model name     |
|1 |100 |Deluxe         |
|10|100 |Deluxe improved|
|2 |200 |Standard       |
|20|200 |Standard new   |

color

|id|Name|
|2 |Red |
|3 |Blue|

car_colors

|id|car_id|color_id|
|3 |1     |2       |
|4 |2     |2       |
|5 |2     |3       |

The deluxe car was added, and afterwards the “deluxe improved” model was inserted. It’s a new version of the same car (same code). Unfortunately, John Doe forgot to update the car_colors table, so now you want to update that table by inserting the same colors for every same car code.

In the example considered, we’d like to add the tuple “Deluxe improved, red” (because Deluxe and deluxe improved have the same code and Deluxe is available in red) and the tuples “standard new, red” and “standard new, black” for the same reasons.

The PSEUDO-CODE (non-sql) should be something like:
all_cars_and_colors = select * from car left outer join car_colors

for each(this_car:all_cars_and_colors){

    if(all_cars_and_colors.color_id does not exist){
        car_colors_to_copy = select * from car inner join car_colors where car.code=this_car.code

        for each(color_to_copy: car_colors_to_copy){
            insert into car_colors(id,car_id,color_id) VALUES (nextval('id_sequence') ,this_car.id,color_to_copy.color_id)
        }

    }
}

How would one solve this using SQL?

  • 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-07T07:24:53+00:00Added an answer on June 7, 2026 at 7:24 am
    -- spoiler
    INSERT INTO car_colors (car_id, color_id)
    SELECT c1.id
            , co.color_id
    FROM car c1
    JOIN car c0 ON 1=1
    JOIN car_colors co ON co.car_id = c0.id
    WHERE c1. zname = 'Deluxe improved'
    AND c0. zname = 'Deluxe'
            ;
    

    UPDATE: since the requrements appear to have changed, here’s a new one. CTE to the resque …

    DROP TABLE car ;
    CREATE TABLE car
            ( id INTEGER NOT NULL PRIMARY KEY
            , zcode integer NOT NULL
            , zname varchar
            );
    INSERT INTO car(id, zcode,zname) VALUES
    (1 ,100 , 'Deluxe' )
    ,(10,100 ,'Deluxe improved' )
    ,(2 ,200 , 'Standard' )
    ,(20,200 , 'Standard new' )
            ;
    
    DROP TABLE color ;
    CREATE TABLE color
            ( id integer NOT NULL PRIMARY KEY
            , zname varchar
            );
    
    INSERT INTO color(id,zname) VALUES
    (2 ,'Red' ) , (3 ,'Blue' )
            ;
    
    DROP TABLE car_colors;
    CREATE TABLE car_colors
            ( id SERIAL NOT NULL PRIMARY KEY
            , car_id  integer NOT NULL REFERENCES car (id)
            , color_id  integer NOT NULL REFERENCES color (id)
            , UNIQUE (car_id,color_id)
            )
            ;
    INSERT INTO car_colors (car_id, color_id) VALUES
      (1,2) , (2,2) , (2,3)
            ;
    
    WITH carmap AS (
            SELECT c0.id AS orgcar
                    , c1.id AS newcar
            FROM car c1
            -- This is an ugly join based on a substring
            JOIN car c0 ON c1.zname ~ c0.zname AND c1.id <> c0.id
            )
    INSERT INTO car_colors (car_id, color_id)
    SELECT cm.newcar
            , co.color_id
    FROM carmap cm
    JOIN car_colors co ON co.car_id = cm.orgcar
    WHERE NOT EXISTS ( SELECT *
            FROM car_colors nx
            WHERE nx.car_id = cm.newcar
            AND nx.color_id = co.color_id
            )
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to perform a sort operation on some field. Is it advantageous making
I want to perform simple validation against multiple fields. Please note these fields are
I want to perform a small SQL server search in my ASP.NET web project.
I want perform a easy task in android, which when receiving a call the
I want to perform an jquery request to php and then I want php
I want to perform action on change in count of rows of an HTML
I want to perform calculations for each company number in the column PERMNO of
I want to perform a segue, after a assync call to a service. In
I want to perform changes in the code which was developed using iOS 3.0,
I want to perform rubyf action in VIM when I press F5 if the

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.