从自定义子查询中选择列名称/别名列表

时间:2013-06-19 18:43:27

标签: oracle list format subquery alias

在Oracle中,有没有办法选择从带有别名的自定义查询返回的所有列?举个例子,假设我们有一个查询如下:

    SELECT FIRST_NAME AS COL1, LAST_NAME AS COL2, ADDRESS AS COL3
    FROM PEOPLE

我想知道是否可以返回一个封装查询:

    COL1
    COL2
    COL3

1 个答案:

答案 0 :(得分:4)

以下是如何在PL / SQL中执行此操作。不知道是否可以使用直接的Oracle SQL。如果需要,你总是可以将它封装在某种功能中。

DECLARE
   TYPE RefCursor_Type IS REF CURSOR;

   D_RefCur             RefCursor_Type;
   D_DescriptionTable   DBMS_SQL.DESC_TAB2;
   D_ColumnCount        INTEGER;
   D_CursorHandle       INTEGER;
BEGIN
   OPEN D_RefCur
   FOR 'SELECT FIRST_NAME AS COL1, LAST_NAME AS COL2, ADDRESS AS COL3 FROM PEOPLE';

   D_CursorHandle := DBMS_SQL.to_cursor_number (D_RefCur);

   DBMS_SQL.DESCRIBE_COLUMNS2 (D_CursorHandle,
                               D_ColumnCount,
                               D_DescriptionTable);


   FOR idx IN 1 .. D_ColumnCount
   LOOP
      DBMS_OUTPUT.put_line (D_DescriptionTable (idx).col_name);
   END LOOP;
END;