For循环的光标

时间:2016-08-10 05:49:26

标签: oracle plsql plsqldeveloper

如何组合下面这两个示例游标,以便它们检索所有4个条件的数据:all,candy,cake,popcorn;在for循环?

我有3个会话/全局变量,可以是空字符串


    s_Candy   Varchar2(5);
    s_Cake    Varchar2(5);
    s_Popcorn Varchar2(5);



    Cursor My_All_Cur Is 
        ... Gets all values
        Select * From My_Table t Where t.State = 'A';



    Cursor My_Other_Cur Is
         ... Gets data by condition
         From My_Table t
         Where t.State = 'A'
           And (t.Candy_Lovers = s_Candy
           Or t.Cake_Lovers = s_Cake
           Or t.Popcorn_Lovers = s_Popcorn);



    For i In My_Unified_Cur Loop
      ... do stuff
    End Loop;

1 个答案:

答案 0 :(得分:1)

解决方案是联合所有2个游标并通过

s_All_Id
来调整它们 也是一个全局变量。


    s_Candy   Varchar2(5);
    s_Cake    Varchar2(5);
    s_Popcorn Varchar2(5);
    s_All_Id  Varchar2(1) := 'N';


    If s_Candy Is Null And s_Cake Is Null And s_Popcorn Is Null Then
      s_All_Id := 'Y';
    End If;


    Cursor My_All_Cur Is 
        Select * 
          From My_Table t 
          Where s_All_Id = 'Y'
            And t.State = 'A'
        Union All
        Select *
          From My_Table t
            Where s_All_Id = 'N'
              And t.State = 'A'
              And (t.Candy_Lovers = s_Candy
              Or t.Cake_Lovers = s_Cake
              Or t.Popcorn_Lovers = s_Popcorn);