0

任何人都可以帮助我或提供我必须根据条件循环遍历集合的示例代码。就像 sql 中的 where 子句一样。非常感谢您的帮助....

这是我的代码:

create table MODEL1
(
  model_id  NUMBER ,
  model_seq NUMBER,
  p_ind     VARCHAR2(1)
);

insert into model1 (MODEL_ID, MODEL_SEQ, P_IND)
values (4, 103, 'U');

insert into model1 (MODEL_ID, MODEL_SEQ, P_IND)
values (3, 102, 'P');

insert into model1 (MODEL_ID, MODEL_SEQ, P_IND)
values (2, 101, 'U');

insert into model1 (MODEL_ID, MODEL_SEQ, P_IND)
values (1, 100, 'P');

MODEL PROCEDURE......

procedure (  param1,param2)        ( assume this procedure is being called from other procedure and collection has been populated already)

TYPE  l_tab is table of  MODEL1%rowtype;

begin

loop through l_tab records where  ltab.model_id=param1  and  p_ind =p

Join based on if else condition.

if   param2 is not null then

   l_tab.model_seq=param2 and ltab.p_ind='P'

if param2 is null then
         l_tab.p_ind='P'          etc...........
4

2 回答 2

0

如果您可以保证您的集合是密集的,那么您可以简单地(在 Oracle 10 或 11g 中):

for idx in 1..my_collection.last LOOP
     if my_collection(idx).model_id = param1 and my_collection(idx).p_ind = 'p' then
              my_collection(idx).some_col = 'some new value';  -- you change the collection
     end if;
end loop;
于 2013-10-12T15:11:54.437 回答
0

“就像 where 子句”的答案是肯定的和否定的。

否:您遍历集合并使用该IF语句检查条件。

是的:但是如果你真的想使用 where 子句,你可以使用 oracle 流水线函数来做到这一点。尽管我认为出于您的目的,这有点矫枉过正。

你会以类似的东西结束

select * from table(myfunction) where model_seq = param1

这是如何工作的有点复杂。当我上次使用它时,我对这个链接非常满意http://www.oracle-base.com/articles/misc/pipelined-table-functions.php

于 2013-10-12T22:57:40.410 回答