pl / sql simple for loop cursor给出了错误

时间:2014-01-28 19:59:25

标签: oracle plsql syntax-error

我收到一条消息“子程序或游标'CUR'引用超出范围”。我不明白什么是超出范围。请指教。

set serveroutput ON
Declare
    CURSOR cur IS 
            select tt.id 
        from (select * from DUMMY_1 t
            where t.status=976 and t.series_Value<'7-%'  
            ) tt   
        where not exists (select *
                from DUMMY_2 d, DUMMY_1 ss_beg, DUMMY_1 ss_end
                where d.status=976 and d.for_class_loc_project=1
                    and ss_beg.id=d.beg_series
                    and ss_end.id=d.end_Series
                    and ss_beg.discharge_subsys = tt.discharge_subsys
                    and ss_beg.line_loop = tt.line_loop
                    and ((d.beg_series=tt.id and d.beg_Station<=tt.beg_station) or      ss_beg.series_value<tt.series_value)
                    And ((D.End_Series=Tt.Id And D.End_Station> Tt.Beg_Station) Or      Ss_End.Series_Value>Tt.Series_Value) 
                );


Begin
for indx in 1..cur.count
     Loop
            Exit when cur.count = 0;
        DBMS_OUTPUT.PUT_LINE('Hello' || cur(indx));
     End Loop;
END;

2 个答案:

答案 0 :(得分:3)

你最好使用隐式游标,形式为:

begin
  for my_data in (
    select col1,
           col2
    from   ...
    where  ...)
  loop
    DBMS_Output.Put_Line(my_data.col1)
  end loop;
end;

此处循环游标的语法:http://docs.oracle.com/cd/E18283_01/appdev.112/e17126/cursor_for_loop_statement.htm

答案 1 :(得分:1)

Hello i have modified the query a lilte bit you can use this. Please let me know for any other queries. Thanks

SET serveroutput ON
DECLARE
  CURSOR cur
  IS
    SELECT tt.id
    FROM
      (SELECT * FROM DUMMY_1 t WHERE t.status=976 AND t.series_Value<'7-%'
      ) tt
  WHERE NOT EXISTS
    (SELECT *
    FROM DUMMY_2 d,
      DUMMY_1 ss_beg,
      DUMMY_1 ss_end
    WHERE d.status              =976
    AND d.for_class_loc_project =1
    AND ss_beg.id               =d.beg_series
    AND ss_end.id               =d.end_Series
    AND ss_beg.discharge_subsys = tt.discharge_subsys
    AND ss_beg.line_loop        = tt.line_loop
    AND ((d.beg_series          =tt.id
    AND d.beg_Station          <=tt.beg_station)
    OR ss_beg.series_value      <tt.series_value)
    AND ((D.End_Series          =Tt.Id
    AND D.End_Station           > Tt.Beg_Station)
    OR Ss_End.Series_Value      >Tt.Series_Value)
    );
BEGIN
  FOR indx IN cur
  LOOP
    DBMS_OUTPUT.PUT_LINE('Hello' || indx);
  END LOOP;
END;