Oracle存储过程中的内部查询

时间:2015-08-25 14:19:28

标签: sql oracle stored-procedures

尝试编写存储过程。我在存储过程中有一个光标,它产生以下输出。

 ID1 | Range | Action | Version | Name
--------------------------------------
  1  | 1-3,  |        |         |
     | 5-7   |   D    |   V0    |  U1
  2  | 10-11 |   A    |   V0    |  U2
  3  | 15-18 |   A    |   V1    |  U3

范围是另一个表ID的{​​{1}}。

UPCListDetails

现在我需要使用范围列查询 ID | UPCName -------------- 1 | Test 2 | Test1 3 | Test2 10 | Test10 ... 表并生成输出

UPCListDetails

基本上我需要查询UPCListDetails表的范围(范围列中的低值和高值)并将其存储在某个变量中,然后再将值注入结果中。我正在阅读有关PLSQL集合的内容,但我无法理解它。

2 个答案:

答案 0 :(得分:0)

最好使用2列和每行范围的行来维护范围:

ID1  Range_From  Range_To  Action Version Name
1    1           3         D      V0      U1
2    5           7         D      V0      U1
3    10          11        A      V0      U2
4    15          18        A      V1      U3

然后你只需要一个像

这样的查询
select u.ID, u.UPCName, r.Action r.Version r.Name
from   UPCListDetails u
       join range_table r
       on u.ID between r.range_from and r.range_to;

这样你就不必解析范围和范围列表。

答案 1 :(得分:0)

如果您能够在程序中更改光标以便以@TonyAndrews'解决方案建议的格式输出数据,那么我建议您继续使用。

但是,如果您坚持使用该光标输出,那么您需要完成将其转换为该格式的工作:

with      output as (select 1 id1, '1-3, 5-7' range, 'D' action, 'V0' version, 'U1' name from dual union all
                     select 2 id1, '10-11' range, 'A' action, 'V0' version, 'U2' name from dual union all
                     select 3 id1, '15-18' range, 'A' action, 'V1' version, 'U3' name from dual),
         main_op as (select id1,
                            regexp_substr(regexp_substr(replace(range, ' '), '[^,]+', 1, level), '[^-]+', 1, 1) range_from,
                            regexp_substr(regexp_substr(replace(range, ' '), '[^,]+', 1, level), '[^-]+', 1, 2) range_to,
                            action,
                            version,
                            name
                     from   output
                     connect by prior id1 = id1
                                and level <= regexp_count(range, ',') + 1
                                and prior dbms_random.value is not null),
  upclistdetails as (select 1 id, 'Test' upcname from dual union all
                     select 2 id, 'Test1' upcname from dual union all
                     select 3 id, 'Test2' upcname from dual union all
                     select 10 id, 'Test10' upcname from dual)
select mo.id1,
       uld.id,
       uld.upcname,
       mo.action,
       mo.version,
       mo.name
from   upclistdetails uld
       inner join main_op mo on uld.id between mo.range_from and mo.range_to;

       ID1         ID UPCNAME ACTION VERSION NAME
---------- ---------- ------- ------ ------- ----
         1          1 Test    D      V0      U1  
         1          2 Test1   D      V0      U1  
         1          3 Test2   D      V0      U1  
         2         10 Test10  A      V0      U2 

main_op子查询的作用是首先为每个id1生成尽可能多的行,因为有范围块(例如,对于id1 = 1,有2个范围块),在每个行上输出每个块。

然后我们取连字符前的元素并将其用作“range_from”列,同样我们将连字符后的元素用于“range_to”。

一旦输出看起来像这样,只需将其加入upclistdetails表即可获得最终结果。

相关问题