declare
  flag number;
  type type_array is varray(10) of varchar2(20);
  var_array type_array := type_array(); 

  --游标2
  cursor cur_test is
    SELECT code CODE
      FROM t_express_model
     where status = 1
       and type = 1;
  cur_record cur_test%ROWTYPE;

BEGIN  
--游标循环
   FOR cr IN (SELECT code CODE
               FROM t_express_model
              where status = 1
                and type = 1) LOOP
    select sum(1) 
      into flag
      from t_waybill_number t
     where t.model_code = cr.code
       and t.status = 0;
    if flag <= 100000 then
     -- Pr_auto_waybillnum(EMNO => cr.code, nums => 1000);
     -- commit;
      DBMS_OUTPUT.PUT_LINE(cr.code || '  Success!!!');
    end if;
  end loop; 

--或者
 FOR cr IN cur_test LOOP
     ...
  end loop; 

 -- 游标的另一种
  OPEN cur_test;
  FETCH cur_test  INTO cur_record;
     DBMS_OUTPUT.PUT_LINE(cur_record.code || '  Success!!!');
  CLOSE cur_test;
 

  --  for 循环
   FOR i IN 1 .. 99 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
   end loop; 
   
   -- 数组循环
   var_array  := type_array('aaaa','jjh','wsb','csl','dd','bb'); 
    for i in 1..var_array.count loop
           DBMS_OUTPUT.PUT_LINE(var_array(i));
      end loop;
END  ;

 

2. 返回游标 ,多行记录

CREATE OR REPLACE FUNCTION F_GetCursorList(P_USER IN VARCHAR2 --接收输入参数
                                         ) RETURN SYS_REFCURSOR AS
  P_RESULT_SET_O SYS_REFCURSOR; --返回游标
  X_SQL          VARCHAR2(200);
BEGIN
  X_SQL := 'select * from t_client where name like ''%' || P_USER || '%'''; --生成SQL语句
  OPEN P_RESULT_SET_O FOR X_SQL;
  RETURN P_RESULT_SET_O; --返回游标
END F_GetCursorList;

 

 

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐