Is there a generic workaround to express a derived column list in Oracle (and MySQL)?

后端 未结 3 1519
时光说笑
时光说笑 2021-02-19 00:20

Many SQL databases support what the SQL standard calls a . Such databases include at least CUBRID, Derby, Firebird, HSQLDB, Postgres, SQL

3条回答
  •  轮回少年
    2021-02-19 00:56

    Since you MUST know the number of columns, but not necessarily the column names, you can use the WITH clause to rename these columns as you wish. For example (WITH works in Oracle and SQL Server, don't have MySQL instance handy):

    WITH t(x,y,z) as (select * from TABLE(fn_returning_xcols(3)))
    select * from t;
    

    Here we don't know the column names in the inner select, but we can rename them in outer WITH clause.

    Another example using a PIVOT in Oracle:

    WITH t(a,b,c,d,e) as 
    (
     select * from 
     (
      select level as levl from dual connect by level <= 5
     )
     PIVOT(max(levl) as l for levl in (1,2,3,4,5))
    )
    select * from t;
    

    Again, we don't care what the inner select column names are (the inner pivot creates somewhat odd column names), we just need to know how many columns and we can rename.

提交回复
热议问题