比如有A,B,C三个小区,每个小区有一些人,如下所示:
A B C
-----------
A1 B1 C1
A2 C2
A3
A4
(A区有4人,名字叫A1,A2,A3,A4,B区有1个人,C区有2个人)
现在要选志愿者N人,选取算法为轮流从每个区抽1人,直至抽满N人。例如N=6,抽取过程为
A1, B1, C1, A2, C2, A3
我想用一个procedure来完成这个事,procedure的输入为N,输出为N行,而且最好不要什么dbms_output.putline,就和select * from table where rownum <= N一样,接下来C#程序还要用这些行呢。
谢谢!

解决方案 »

  1.   


    create or replace procedure Cx(N in number,Pc out sys_refcursor) is
    v_sql varchar2(2000);
    begin
      v_sql:='select A from (select A,rownum rn from yourTable where A is not null
                    union all select B,rownum rn from yourTable where B is not null
                    union all select C,rownum rn from yourTable where C is not null
                    order by 2,1) where rownum<='||N;
    open Pc for v_sql;
    end;
      

  2.   

    create procedure p_test(prm_OUTRESULT OUT sys_refcursor) IS
    begin
      OPEN prm_OUTRESULT FOR
        SELECT col1, col2 FROM table1;
    END;