基于动态信息对嵌套表进行排序

时间:2017-04-26 17:02:05

标签: oracle dynamic-sql nested-table

我无法根据order by子句中的一些动态信息对嵌套表进行排序。

以下是我找到的内容示例(https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-quite-simple-way-part-two-have-the-sql-engine-do-the-heavy-lifting/

这里唯一的区别是我需要在order by子句

中动态定义列和方向
SELECT CAST(MULTISET(SELECT * 
                       FROM TABLE(table_a) 
                      ORDER BY P_SORT_COLUMN P_DIRECTION 
                     ) as table_typ) 
  INTO table_b
  FROM dual;

所以,为了避开我认为我想使用动态SQL并将其放在proc中,因为表单不能动态地执行此操作

loc_sql_stmt VARCHAR2(500);



BEGIN

    loc_sql_stmt := 'SELECT CAST(MULTISET(SELECT * ' ||
                                           'FROM TABLE(P_TABLE_A) ' ||
                                          'ORDER BY P_COLUMN P_DIRECTION || ) as table_typ) ' || 
                            'INTO P_TABLE_B' || 
                            'FROM dual;';


    EXECUTE IMMEDIATE loc_sql_stmt
             USING IN P_TABLE_A, P_COLUMN, P_DIRECTION, P_TABLE_B;

END;

我从EXECUTE IMMEDIATE行得到的错误是“ORA-00936缺失表达式

那么是否有更好的方法可以按任何给定列和方向对嵌套表进行排序,或者如何让这个动态SQL工作?

以下是一个示例:

在DB中创建:

  CREATE OR REPLACE TYPE table_obj AS OBJECT(
                    column1       VARCHAR2(20),
                    column2     VARCHAR2(20));

  CREATE OR REPLACE TYPE table_typ AS TABLE OF table_obj;  

然后运行一个示例:

DECLARE
    table_a           table_typ := table_typ ();
   table_b           table_typ := table_typ ();
   loc_idx           NUMBER;
   loc_sort_column   INTEGER := 1;
   loc_desc          VARCHAR2 (4);
   P_SORT_COLUMN     VARCHAR2 (100) := 'column1';
   P_DIRECTION       VARCHAR2 (4) := 'DESC';
   loc_sql_stmt      VARCHAR2 (500);
BEGIN
   FOR i IN 1 .. 5
   LOOP
      loc_idx := table_a.COUNT + 1;
      table_a.EXTEND;
      table_a (loc_idx) := table_obj (NULL, NULL);

      table_a (loc_idx).column1 := TO_CHAR (loc_idx);
      table_a (loc_idx).column2 := TO_CHAR (loc_idx);
   END LOOP;

   --
   loc_sql_stmt :=
     'SELECT CAST(MULTISET(SELECT * ' || 
                            'FROM TABLE(' || table_a || ') ' || 
                           'ORDER BY ' || P_SORT_COLUMN || ' '|| P_DIRECTION || 
                        ' ) as table_typ) ' || 
       'INTO :table_b' || 
       'FROM dual';

  EXECUTE IMMEDIATE loc_sql_stmt USING IN OUT table_a, table_b;

 FOR i IN 1 .. table_b.COUNT
 LOOP
  DBMS_OUTPUT.PUT_LINE (table_b (i).rx_number);
 END LOOP;
END; 

2 个答案:

答案 0 :(得分:1)

要将变量传递给本机动态SQL,请在参数名称前使用:,以构建动态语句使用连接,如下所示

loc_sql_stmt VARCHAR2(500);

BEGIN

    loc_sql_stmt := 'SELECT CAST(MULTISET(SELECT * ' ||
                                           'FROM TABLE('|| P_TABLE_A || ') ' ||
                                          'ORDER BY ' ||  P_COLUMN || ', ' || P_DIRECTION || ' ) as table_typ) ' || 
                            'INTO :P_TABLE_B' || 
                            'FROM dual;';


    EXECUTE IMMEDIATE loc_sql_stmt
             USING OUT P_TABLE_B;
END;

EDITED版本:

现在看到你的代码,我明白你需要什么。为了使它工作,我们需要使用动态PL / SQL块,而不是Native SQL这里是你的样本的工作代码,并注意什么是变量和什么是连接文字

DECLARE
   table_a table_typ := table_typ();
   table_b table_typ := table_typ();
   loc_idx NUMBER;
   loc_sort_column INTEGER := 1;
   loc_desc VARCHAR2(4);
   P_SORT_COLUMN VARCHAR2(100) := 'column1';
   P_DIRECTION VARCHAR2(4) := 'desc';
   loc_sql_stmt VARCHAR2(500);
BEGIN
   FOR i IN 1 .. 5
   LOOP
      loc_idx := table_a.COUNT + 1;
      table_a.EXTEND;
      table_a(loc_idx) := table_obj(NULL, NULL);

      table_a(loc_idx).column1 := TO_CHAR(loc_idx);
      table_a(loc_idx).column2 := TO_CHAR(loc_idx);
   END LOOP;

   --
   loc_sql_stmt := 'begin SELECT CAST(MULTISET(SELECT * ' ||
                   'FROM TABLE(:table_a ) ORDER BY ' || P_SORT_COLUMN || ' ' ||
                   P_DIRECTION || ' ) as table_typ ) ' || ' INTO :table_b ' ||
                   'FROM dual; end;';

   EXECUTE IMMEDIATE loc_sql_stmt
      USING table_a, IN OUT table_b;

   FOR i IN 1 .. table_b.COUNT
   LOOP
      DBMS_OUTPUT.PUT_LINE(table_b(i).column1);
   END LOOP;
END;

答案 1 :(得分:0)

如果您选择了有限的列/方向,请在订单中尝试使用case语句,例如:

select * from tab
order by case when :order = 'c1_asc'  then c1 else null end asc
      ,  case when :order = 'c1_desc' then c1 else null end desc
      ,  case when :order = 'c2_asc'  then c2 else null end asc
      ,  case when :order = 'c2_desc' then c2 else null end desc
/* ... */
;