Best way to do multi-row insert in Oracle?

前端 未结 9 1391
悲&欢浪女
悲&欢浪女 2020-11-22 02:52

I\'m looking for a good way to perform multi-row inserts into an Oracle 9 database. The following works in MySQL but doesn\'t seem to be supported in Oracle.



        
相关标签:
9条回答
  • 2020-11-22 03:44

    This works in Oracle:

    insert into pager (PAG_ID,PAG_PARENT,PAG_NAME,PAG_ACTIVE)
              select 8000,0,'Multi 8000',1 from dual
    union all select 8001,0,'Multi 8001',1 from dual
    

    The thing to remember here is to use the from dual statement.

    (source)

    0 讨论(0)
  • 2020-11-22 03:45

    If you have the values that you want to insert in another table already, then you can Insert from a select statement.

    INSERT INTO a_table (column_a, column_b) SELECT column_a, column_b FROM b_table;
    

    Otherwise, you can list a bunch of single row insert statements and submit several queries in bulk to save the time for something that works in both Oracle and MySQL.

    @Espo's solution is also a good one that will work in both Oracle and MySQL if your data isn't already in a table.

    0 讨论(0)
  • 2020-11-22 03:46

    you can insert using loop if you want to insert some random values.

    BEGIN 
        FOR x IN 1 .. 1000 LOOP
             INSERT INTO MULTI_INSERT_DEMO (ID, NAME)
             SELECT x, 'anyName' FROM dual;
        END LOOP;
    END;
    
    0 讨论(0)
提交回复
热议问题