【问题标题】:parameterized cursors in PL/SQLPL/SQL 中的参数化游标
【发布时间】:2014-02-17 09:00:52
【问题描述】:

我在 sql developer 中运行以下代码时出现以下错误。我找不到这段代码有什么问题。 概念:游标 Base1 中的 sql 返回大约 100 行。我想将这些行中的每一行用作目标 cusor 内的 sql 的输入,它会进一步返回几行。 我得到的错误是:

在预期以下之一时遇到符号“(”:. into bulk

遇到符号“;”预期以下情况之一时:。 ( , % 来自

在预期以下情况之一时遇到符号“CLOSE”:end not pragma final instantiable order overriding static 成员构造函数映射

    set serveroutput ON

Declare
    Type Beg_Ser_Tab1 Is Table Of DUMMY%Rowtype Index By Pls_Integer;
    Type Beg_Ser_Tab2 Is Table Of DUMMY%Rowtype INDEX BY PLS_INTEGER;
    L_Beg_Ser_Tab Beg_Ser_Tab1;
    L_Beg_Ser_Tab_Fin Beg_Ser_Tab2;

    result number;

    CURSOR  Base1 IS 

        select * from DUMMY c1
        where status=976 and for_class_loc_project=1
        and not exists 
            (select * from DUMMY c2 
            where c2.status=976 and c2.for_class_loc_project=1
            and c2.end_series=c1.COLOUMN_X and c2.end_station=c1.beg_station)
        and not exists 
            (select * from mv_station_Series t 
            where t.status in (SELECT ID FROM list_domain 
            WHERE LOWER (domainvalue) IN ('active', 'preliminary as-built', 'idle', 'construction'))
            and c1.COLOUMN_X=t.id and c1.beg_station=t.beg_station 
            )     
        order by c1.id;



    CURSOR  target(v_id NUMBER) IS

        Select *  
            From DUMMY Where COLOUMN_X in (
            Select ID From Station_Series Where 
            Status = 976
            And Discharge_Subsys = (Select Discharge_Subsys From Station_Series Where Id = v_id ) 
            And Line_Loop = (Select Line_Loop From Station_Series Where Id = v_id)) And Status In
            (Select Id From List_Domain 
            Where Lower (Domainvalue) In ('active', 'preliminary as-built', 'idle', 'construction'))
            Order By Beg_Station Asc;

BEGIN
    OPEN Base1;
    FETCH Base1 BULK COLLECT INTO l_beg_ser_tab1;
    EXIT WHEN l_beg_ser_tab1.count = 0;
    FOR index1 IN 1..l_beg_ser_tab1.count
        LOOP
        Dbms_Output.Put_Line('For Beg Series '|| L_Beg_Ser_Tab1(Index1));
        Open Target(L_Beg_Ser_Tab1.COLOUMN_X);
        FETCH target(l_beg_ser_tab1.COLOUMN_X) BULK COLLECT INTO l_beg_ser_tab_fin;
        FOR index2 IN 1..l_beg_ser_tab_fin.count
            LOOP
            DBMS_OUTPUT.PUT_LINE('              '||l_beg_ser_tab_fin(index2));
            END LOOP;
        CLOSE target;

        DBMS_OUTPUT.PUT_LINE('---------------------------------------------------------');
        END LOOP;
    CLOSE Base1;
END 

【问题讨论】:

  • “列”?此外,您正在使用许多不相关的子查询而不是直接连接。有什么理由吗?
  • 我没有使用 Joins,因为我只是想获得当天的解决方案,而不必过多担心性能问题。是的,就像你说的,加入让事情变得容易,而且,我的代码看起来很笨拙。我将练习连接并尝试适应此代码。

标签: oracle plsql


【解决方案1】:

我想一旦你有:

Open Target(L_Beg_Ser_Tab1.COLOUMN_X);

您无需在以下位置再次指定参数:

FETCH target(l_beg_ser_tab1.COLOUMN_X) B

此外,隐式游标通常更快,更容易编码。为什么不使用它们?事实上,为什么不对整个逻辑使用一个隐式游标呢?

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多