How to select multiple rows filled with constants?

后端 未结 15 1055
夕颜
夕颜 2020-11-29 17:08

Selecting constants without referring to a table is perfectly legal in an SQL statement:

SELECT 1, 2, 3

The result set that the latter retu

15条回答
  •  感动是毒
    2020-11-29 17:34

    Here is how I populate static data in Oracle 10+ using a neat XML trick.

    create table prop
    (ID NUMBER,
     NAME varchar2(10),
     VAL varchar2(10),
     CREATED timestamp,
     CONSTRAINT PK_PROP PRIMARY KEY(ID)
    );
    
    merge into Prop p
    using (
    select 
      extractValue(value(r), '/R/ID') ID,
      extractValue(value(r), '/R/NAME') NAME,
      extractValue(value(r), '/R/VAL') VAL
    from
    (select xmltype('
    
       1key1value1
       2key2value2
       3key3value3
    
    ') xml from dual) input,
     table(xmlsequence(input.xml.extract('/ROWSET/R'))) r
    ) p_new
    on (p.ID = p_new.ID)
    when not matched then
    insert
    (ID, NAME, VAL, CREATED)
    values
    ( p_new.ID, p_new.NAME, p_new.VAL, SYSTIMESTAMP );
    

    The merge only inserts the rows that are missing in the original table, which is convenient if you want to rerun your insert script.

提交回复
热议问题