在动态查询的最后一条记录的末尾删除“全部合并”

时间:2018-11-14 12:20:07

标签: oracle stored-procedures plsql dynamicquery

对于我的问题,我建立了一个简单的例子来说明我的问题。

比方说,您有一个动态查询,该查询会生成几个带有UNION ALL的选择语句。有没有办法防止最后一条记录末尾的“ UNION ALL”出现?

我的例子:

CREATE OR REPLACE PROCEDURE PROC_TEST AS 
BEGIN
    DECLARE 
        DDL_STRING CLOB;
    BEGIN
        FOR x IN (SELECT TABLE_NAME FROM HLP_TABLES WHERE ENABLED = 1)
        LOOP
            DDL_STRING := 'SELECT ID FROM ' || x.TABLE_NAME || ' UNION ALL ';
            DBMS_OUTPUT.PUT_LINE(DDL_STRING);
        END LOOP;
    END;
END PROC_TEST;

4 个答案:

答案 0 :(得分:3)

如果您确定ddl的总长度不超过4000个字符,则可以使用LISTAGG并避免循环。

CREATE OR REPLACE PROCEDURE proc_test
     AS
     DECLARE
          ddl_string   CLOB;
     BEGIN
          SELECT
               LISTAGG('SELECT ID FROM ' || table_name,' UNION ALL ') WITHIN GROUP(
                    ORDER BY table_name)
          INTO ddl_string 
          FROM HLP_TABLES
     WHERE enabled = 1;
          dbms_output.put_line(ddl_string);
END proc_test;
/

答案 1 :(得分:2)

执行以下操作:

CREATE OR REPLACE PROCEDURE PROC_TEST AS 
   DDL_STRING CLOB;
BEGIN
    FOR x IN (SELECT TABLE_NAME FROM HLP_TABLES WHERE ENABLED = 1)
    LOOP
        DDL_STRING := DDL_STRING||' SELECT ID FROM ' || x.TABLE_NAME || ' UNION ALL';
    END LOOP;
    DDL_STRING := REGEXP_REPLACE(DDL_STRING, ' UNION ALL$');
    DBMS_OUTPUT.PUT_LINE(DDL_STRING);
END PROC_TEST;

答案 2 :(得分:1)

您可以在循环中使用counter来确定是否需要UNION ALL。 试试这个:

  CREATE OR REPLACE PROCEDURE PROC_TEST AS
  BEGIN
    DECLARE
      v_counter  number := 1;
      DDL_STRING CLOB;
    BEGIN
      FOR x IN (SELECT TABLE_NAME FROM HLP_TABLES WHERE ENABLED = 1) LOOP

        DDL_STRING := case
                        when v_counter > 1 then
                         ' UNION ALL ' end || ' SELECT ID FROM ' || x.TABLE_NAME;
        DBMS_OUTPUT.PUT_LINE(DDL_STRING);
        v_counter := v_counter + 1;
      END LOOP;
    END;
  END PROC_TEST;

答案 3 :(得分:0)

在生成这样的代码时,我通常使用简单的if语句,例如:

create or replace procedure proc_test as 
  ddl_string clob;
begin

  for x in (select table_name from hlp_tables where enabled = 1)
  loop

    if ddl_string is not null then
      ddl_string := ddl_string || ' union all ';
    end if;

    ddl_string := ddl_string || ' select id from ' || x.table_name;

  end loop;

  dbms_output.put_line(ddl_string);
end proc_test;