How to select multiple rows filled with constants?

后端 未结 15 884
夕颜
夕颜 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('
    <ROWSET>
       <R><ID>1</ID><NAME>key1</NAME><VAL>value1</VAL></R>
       <R><ID>2</ID><NAME>key2</NAME><VAL>value2</VAL></R>
       <R><ID>3</ID><NAME>key3</NAME><VAL>value3</VAL></R>
    </ROWSET>
    ') 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.

    0 讨论(0)
  • 2020-11-29 17:37
    SELECT * 
    FROM DUAL 
    CONNECT BY ROWNUM <= 9;
    
    0 讨论(0)
  • 2020-11-29 17:37

    An option for DB2:

    SELECT 101 AS C1, 102 AS C2 FROM SYSIBM.SYSDUMMY1 UNION ALL
    SELECT 201 AS C1, 202 AS C2 FROM SYSIBM.SYSDUMMY1 UNION ALL
    SELECT 301 AS C1, 302 AS C2 FROM SYSIBM.SYSDUMMY1
    
    0 讨论(0)
  • 2020-11-29 17:38

    Oracle. Thanks to this post PL/SQL - Use "List" Variable in Where In Clause

    I put together my example statement to easily manually input values (being reused in testing an application by testers):

    WITH prods AS (
        SELECT column_value AS prods_code 
        FROM TABLE(
            sys.odcivarchar2list(
                'prod1', 
                'prod2'
            )
        )
    )
    SELECT * FROM prods
    
    0 讨论(0)
  • 2020-11-29 17:41
    select (level - 1) * row_dif + 1 as a, (level - 1) * row_dif + 2 as b, (level - 1) * row_dif + 3 as c
        from dual 
        connect by level <= number_of_rows;
    

    something like that

    select (level - 1) * 3 + 1 as a, (level - 1) * 3 + 2 as b, (level - 1) * 3 + 3 as c
        from dual 
        connect by level <= 3;
    
    0 讨论(0)
  • 2020-11-29 17:45

    Try the connect by clause in oracle, something like this

    select level,level+1,level+2 from dual connect by level <=3;
    

    For more information on connect by clause follow this link : removed URL because oraclebin site is now malicious.

    0 讨论(0)
提交回复
热议问题