用于循环的plsql游标仅返回ID的最后一条记录

问题描述 投票:0回答:1

我的光标选择子句是

>    CURSOR get_address_upd_c (
>         id t1.id%TYPE
>     ) IS
>     SELECT
>         street_line1,
>         street_line2,
>         city,
>         stat_code,
>         zip,
>         activity_date,
>         atyp_code
>     FROM
>         addr
>     WHERE
>         addr_im = '1234'
>         AND status_ind IS NULL
>         AND from_date < sysdate
>         AND decode(to_date, NULL, sysdate, to_date) >= sysdate
>     ORDER BY
>         activity_date DESC;

输出返回此ID的4条记录(4个不同的地址)。

我的循环游标仅获取此ID的最后一条记录,相反,我希望它返回ID的所有地址。

BEGIN
for rec in get_address_upd_c(pidm)
loop
   lv_strt_ln1:=rec.street_line1;
   lv_strt_ln2:=rec.street_line2;
   lv_city:=rec.city;
   lv_state:=rec.stat_code;
   lv_zip:=rec.zip;
   lv_addr_actv_date:=rec.activity_date;
   lv_addr_type:=rec.atyp_code;

END LOOP;
END;

然后打印到o / p文件我的程序中有太多循环游标的方法。我应该采用批量收集来实现这一目标吗?

plsql cursor plsqldeveloper plsql-package
1个回答
0
投票
 My cursor for loop fetches only the last record for this id, instead i
 want it to return all the addresses for the id
 . 
 . 
 . 
 . 
 then print to
 the o/p file I have way too many loops an cursors inside my procedure.
 Should i go with bulk collect to achieve this?

使用批量操作在循环过程中总是很有用,但是,如果要使用循环,则可以使用dbms_output程序包PUTLINE过程将Print的结果Loop

declare
  CURSOR get_address_upd_c ( id number) 
   IS
      Select * 
        from 
          ( Select 1 col from dual
            Union all
            Select 1 from dual
            Union all
            Select 1 from dual
            Union all
            Select 2 from dual
            Union all
            Select 2 from dual
            Union all
            Select 3 from dual
            Union all
            Select 3 from dual
           )
          where col = id;

  lv_strt_ln1    number;    
  pidm           number := &1; -- Passing the id at runtime
BEGIN
    FOR rec IN get_address_upd_c(pidm) 
    LOOP
        lv_strt_ln1         := rec.col;

        dbms_output.put_line(lv_strt_ln1 || chr(10));  

    END LOOP;

END;

Exec:

SQL> /
1
1
1
1
PL/SQL procedure successfully completed
© www.soinside.com 2019 - 2024. All rights reserved.