PLSQL游标for/while循环程序

PL SQL cursor for/while loop procedures

你好需要一些关于我分配的一些工作的帮助,但在弄清楚它时遇到了一些困难。

CREATE TABLE "DET_CAM" 
    ("EQUIPMENT_ID" NVARCHAR2(100),
    "EQUIPMENT_TYPE_ID" NUMBER(6),
    "LINK_ID" NUMBER(9), 
    "NODE_FR_ID" NUMBER(9),
    "NODE_TO_ID" NUMBER(9),
    "ROAD_NAME" NVARCHAR2(64),  
    "DIRECTION" NUMBER(1)
    );

Insert Data file

创建了一个 for 循环以在 Main Equipment/Links 中循环。通过使用另外 2 个带参数的循环,我如何找出主块之前和之后的下一个或上一个块。

Link flow how the logic should work

Declare
    -- Cursors --
    Cursor c_MainLink 
    is 
        Select equipment_id, link_id, node_fr_id, node_to_id, road_name, direction
        From DET_CAM
        where 1=1 and equipment_type_id = 113;
    
    -- Downstream Cursor with parameter --
    Cursor c_downlink (p_dn_node_to_id NUMBER, p_dn_direction NUMBER)
    is
        Select a.node_to_id, a.direction, a.equipment_id, a.link_id, a.road_name, a.location_id
        From DET_CAM a
        where 1=1 and equipment_type_id = 113 and a.node_to_id = p_dn_node_to_id and a.direction = p_dn_direction
        Order by road_name asc, direction asc;
        
    -- Upstream Cursor with parameter --
    Cursor c_uplink (p_up_node_fr_id NUMBER, p_up_direction NUMBER)
    is
        Select a.node_fr_id, a.direction, a.equipment_id, a.link_id, a.road_name, a.location_id
        From DET_CAM a
        where 1=1 and equipment_type_id = 113 and a.node_fr_id = p_up_node_fr_id and a.direction = p_up_direction
        Order by road_name asc, direction asc;
        
Begin
    for m_equip in c_MainLink loop
            for ds_equip in c_downlink(m_equip.node_fr_id, m_equip.direction) loop
                for up_equip in c_uplink(m_equip.node_to_id, m_equip.direction) loop
                    dbms_output.put_line('Downstream (equip_ID, Link_ID, RN): '||ds_equip.equipment_id||' | '||ds_equip.link_id||' | '||ds_equip.road_name||
                                         ' | Main (equip_ID, Link_ID, RN): '||ds_equip.equipment_id||' | '||ds_equip.link_id||' | '||ds_equip.road_name||
                                         ' | Upstream (equip_ID, Link_ID, RN): '||up_equip.equipment_id||' | '||up_equip.link_id||' | '||up_equip.road_name
                                         );
                end loop; 
            end loop;            
    end loop;
End;

/* Actual Result:*/
Downstream (equip_ID, Link_ID, RN): DET_516323 | 34854 | BISHAN FLYOVER | Main (equip_ID, Link_ID, RN): DET_516323 | 34854 | BISHAN FLYOVER | Upstream (equip_ID, Link_ID, RN): DET_516325 | 34637 | BISHAN FLYOVER
Downstream (equip_ID, Link_ID, RN): DET_566338 | 167815 | TUAS VIADUCT | Main (equip_ID, Link_ID, RN): DET_566338 | 167815 | TUAS VIADUCT | Upstream (equip_ID, Link_ID, RN): DET_566336 | 167817 | TUAS VIADUCT

/* 预期结果:*/

SQL Combine table

Img 示例:Display both Downstream & Upstream links of an associated equipment that has been selected along with both the equipment details and road name of those links

上行链路块和下行链路块应该彼此相同,只是一个与另一个相反,至少从我对问题的理解来看是这样。 现在发生的事情是它显示了直接相互连接的块,但是如果 2 个块之间有一个洞,它不会在丢失的块之后显示 Next/Previous 块(这是我需要的)。

*请注意,链接和节点 ID 不按顺序....我认为这是我发现它难以正常工作的主要原因之一。

 DECLARE
  CURSOR c_product
  IS
    SELECT 
        product_name, list_price
    FROM 
       products 
    ORDER BY 
    list_price DESC;
BEGIN
  FOR r_product IN c_product
  LOOP
   dbms_output.put_line( r_product.product_name || ': $' ||  r_product.list_price);        
 END LOOP;
END;