无意中看到的一个题目,存储过程计算出字符串‘ABCD’的全部排列组合(比如:ABCD、ACBD、ABDC....)贴在这里大家讨论下,看都有那些解法?

解决方案 »

  1.   

    找到的参考,用SQL实现的。。http://www.itpub.net/thread-763065-1-1.html
      

  2.   

    这题。在学算法时倒是实现过,递归下
    oracle 关注中
      

  3.   

    SQL> with tab as(select substr('ABCD',rownum,1) name from dual connect by rownum<=length('ABCD'))
      2  select a.name||b.name||c.name||d.name from tab a,tab b,tab c,tab d
      3  where a.name<>b.name and a.name<>c.name and a.name<>d.name
      4    and b.name<>c.name and b.name<>d.name
      5    and c.name<>d.name;
     
    A.NAME||B.NAME||C.NAME||D.NAME
    ------------------------------
    ABCD
    ABDC
    ACBD
    ACDB
    ADBC
    ADCB
    BACD
    BADC
    BCAD
    BCDA
    BDAC
    BDCA
    CABD
    CADB
    CBAD
    CBDA
    CDAB
    CDBA
    DABC
    DACB
     
    A.NAME||B.NAME||C.NAME||D.NAME
    ------------------------------
    DBAC
    DBCA
    DCAB
    DCBA
     
    24 rows selected
     
    SQL> 
      

  4.   

    这个其实可以更加普适一点,使用动态sql实现长度不固定的字串排列数
      

  5.   


    是的,在procedure中可以使用execute immediate执行动态sql;SQL> with tab as(select substr('ABCD',rownum,1) value from dual connect by rownum<=length('ABCD'))
      2     select REPLACE(sys_connect_by_path(value, '#'), '#') combo
      3        from tab
      4      where level = 4
      5     connect by nocycle prior value != value
      6            and level <= 4;
     
    COMBO
    --------------------------------------------------------------------------------
    ABCD
    ABDC
    ACBD
    ACDB
    ADBC
    ADCB
    BACD
    BADC
    BCAD
    BCDA
    BDAC
    BDCA
    CABD
    CADB
    CBAD
    CBDA
    CDAB
    CDBA
    DABC
    DACB
     
    COMBO
    --------------------------------------------------------------------------------
    DBAC
    DBCA
    DCAB
    DCBA
     
    24 rows selected
      

  6.   


    create or replace procedure prc_permutation_with(i_string varchar2) as
      /***
       i_string :'ABCD'
      ***/
      v_sql_permutation varchar2(2000);
    begin
      declare
        type type_cur_result is ref cursor;
        cur_result type_cur_result;
        v_element  varchar2(4);
      
      begin
        dbms_output.put_line('The Permutation is:');
      
        v_sql_permutation := 'with tab as(select substr(' || '''' || i_string || '''' ||
                             ',rownum,1) value from dual connect by rownum<=length(' || '''' ||
                             i_string || '''))
        select REPLACE(sys_connect_by_path(value, ''#''), ''#'') combo
           from tab
         where level = 4
        connect by nocycle prior value != value
               and level <= 4';
      
        open cur_result for v_sql_permutation;
        LOOP
          FETCH cur_result
            INTO v_element;
          EXIT WHEN cur_result%NOTFOUND;
          dbms_output.put_line(v_element);
        END LOOP;
        CLOSE cur_result;
      
      exception
        when others then
          raise_application_error(-20005,
                                  'ERROR:' || to_char(SQLCODE) || '-' ||
                                  SQLERRM);
      end;
    end prc_permutation_with;