Oracle之函数中使用游标

create or replace function getcustprodinstaddr(in_CustId in number,in_area_code in number) 
return varchar2 
is
  Result varchar2(4000);
  v_acc_nbr varchar2(400);
  tempCount number:=1;
  type ref_cursor is ref cursor;
  v_cursor ref_cursor;
begin
  Result:='';
  open v_cursor for 
  'select install_addr from tb_prd_prd_inst_'||to_char(in_area_code)||
  ' where PRD_INST_STAS_ID not in(''1003'',''1101'',''1401'',''1102'') 
  and own_cust_id='||to_char(in_CustId)||' order by install_date desc';
  loop
    fetch v_cursor into v_acc_nbr;
    exit when v_cursor%notfound;
	if(tempCount>8) then
		goto label_end;
	end if;
	Result:=v_acc_nbr||','||Result;
	tempCount := tempCount +1;
  end loop;
  <<label_end>>
  close v_cursor;
  return(Result);
  exception 
	when others then
	if(v_cursor%isopen) then
		close v_cursor;
	end if;
    return '';
end getcustprodinstaddr;

posted on 2016-06-09 18:05  菜鸟Z  阅读(279)  评论(0编辑  收藏  举报

导航