Skip to content

How to achieve select in insert query  #4374

Open

Description

@codeninja0x01

Issue type:

[x ] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[x ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

 -- Insert a new record into orders and obtain the new order ID
  INSERT INTO orders (created_on, customer_id, shipping_id, tax_id) VALUES
         (NOW(), inCustomerId, inShippingId, inTaxId);
  -- Obtain the new Order ID
  SELECT LAST_INSERT_ID() INTO orderId;

  -- Insert order details in order_detail table
  INSERT INTO order_detail (order_id, product_id, attributes,
                            product_name, quantity, unit_cost)
  SELECT      orderId, p.product_id, sc.attributes, p.name, sc.quantity,
              COALESCE(NULLIF(p.discounted_price, 0), p.price) AS unit_cost
  FROM        shopping_cart sc
  INNER JOIN  product p
                ON sc.product_id = p.product_id
  WHERE       sc.cart_id = inCartId AND sc.buy_now;

Activity

  1. codebarn4ajay commented on Oct 18, 2019

    @codebarn4ajay

    One of the way, I could find is via query runner. In my case, I was supposed to insert more than 700K records into destination table from source view. My destination table was also rapidly changing and caused integrity constraint violation. so sharing the complete solution in case it make sense to others.

    const qrySelect = "select * from source_view";
    
    const qryInsert= "insert into destination_table";
    
    const queryRunner = await oracleConnection.createQueryRunner();
    
    queryRunner.connect();
    
    await queryRunner.startTransaction();
    
    const results = await queryRunner.query(`${qryInsert}${qrySelect}`);
    
    await queryRunner.commitTransaction();
    
    queryRunner.release()
    
  2. ivid commented on Nov 5, 2019

    @ivid

    Any update on this?

    Aside from doing a raw query, could not find any way to achieve this (INSERT INTO SELECT statement).

  3. lueenavarro commented on Apr 18, 2020

    @lueenavarro

    +1 on this, I want to get away with raw queries as much as possible

  4. ryegye24 commented on Mar 22, 2021

    @ryegye24

    +1 I tried just doing

    await repository
                .createQueryBuilder("<repo name alias>")
                .insert()
                .into(EntityName, [...columns])
                .select([...columns])
                .from(EntityName, "<repo name alias>")
                .where({...whereClause})
                .printSql()
                .execute()

    and it completely dropped the INSERT INTO part and just ran the SELECT

  5. unkindypie commented on Feb 23, 2022

    @unkindypie

    Still relevant

  6. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

  7. Moustafa3attia commented on Mar 16, 2026

    @Moustafa3attia

    Still relevant

  8. reopened this on Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions