代码之家  ›  专栏  ›  技术社区  ›  toolkit

oracle:在输出报告中配置列的有效方法

  •  0
  • toolkit  · 技术社区  · 15 年前

    我正在设计一个HTML报告,它可以有效地从单个表中提取列

    此表中的列数非常大,我想用某种方式配置应用程序,以说明要显示哪些列。注意:这不是按用户设置。

    假设我有主桌:

    MAIN_TABLE
    id
    first_name
    last_name
    weight
    height
    attribute_4
    attribute_5
    attribute_6
    ...
    attribute_99
    

    我在想一张桌子

    MAIN_TABLE_DISPLAY
    column_name
    display
    

    或者也许

    MAIN_TABLE_DISPLAY
    display_id
    display_first_name
    display_last_name
    display_weight
    display_height
    display_attribute_4
    display_attribute_5
    display_attribute_6
    ...
    display_attribute_99
    

    但我想做一个高效的连接。

    有什么建议吗?

    谢谢。

    3 回复  |  直到 15 年前
        1
  •  1
  •   jva    15 年前

    动态列包含/排除==动态SQL。 这个解决方案可能会给你一些建议。 http://tkyte.blogspot.com/2006/01/i-like-online-communities.html -他将ref_游标传递给一个函数,该函数返回一个CLOB,该CLOB是完全格式化的HTML表,包含该ref_游标的完整结果集。不到100行。

        2
  •  0
  •   Jon Onstott    15 年前

    你有没有考虑过使用视图?应用程序可以从中提取数据,若要更改显示的列,请更改视图。您还必须更改事物的表示方面,以考虑不同的列。

        3
  •  0
  •   l0b0    15 年前

    正如jva所说,您需要使用动态SQL。类似这样的事情(通过适当的错误修复)应该可以做到:

    type column_table is table of varchar2(30);
    
    function table_as_html(table_name varchar2, columns column_table) return clob is
      sql_query  varchar2(32767);
      sql_cursor sql_refcursor;
      html_row   clob;
      html_text  clob;
    begin
      sql_query := 'select ''<tr>';
      for column in 1 .. columns.count loop
        sql_query := sql_query||'||''<td>''||'||columns(column)||'||''</td>'''
      end loop;
      sql_query := sql_query||'||''</tr>'' from '||table_name;
    
      open sql_cursor for sql_query;
      loop
        fetch sql_cursor into html_row;
        exit when sql_cursor%notfound;
    
        html_text := html_text||html_row||chr(10);
      end loop;
      close sql_cursor;
    
      return html_text;
    end;