带有where子句的游标

时间:2013-11-11 17:08:51

标签: sql oracle where-clause

我正在尝试使用带有where子句的游标。它与我假设的简单光标不同。我在我的情况下尝试了以下内容,但似乎没有打印任何输出。

    Declare 
      BG_TOTAL number;
     SDO_GEOM varchar2 ;

      cursor c1 is 
        select 
        SDO_GEOM.SDO_AREA(GEOMETRY, 0.005,'unit = SQ_MILE') 
          from <tablename>
          where SITE_ID= 8;

      Type SITE_TAB_TYPE is table of  c1%ROWTYPE;

      SITE_LIST SITE_TAB_TYPE;

    Begin
      open c1;
      FETCH c1 BULK COLLECT INTO SITE_LIST;

      close c1;


         DBMS_OUTPUT.PUT_LINE(SITE_LIST(1).SDO_GEOM);

    Exception
        When others then 
        DBMS_OUTPUT.PUT_LINE(sqlerrm);
    End;



[Error] Execution (3: 11): ORA-06550: line 3, column 11:
PLS-00215: String length constraints must be in range (1 .. 32767)
ORA-06550: line 7, column 5:
PLS-00487: Invalid reference to variable 'SDO_GEOM'
ORA-06550: line 7, column 5:
PLS-00487: Invalid reference to variable 'SDO_GEOM'
ORA-06550: line 22, column 40:
PLS-00302: component 'SDO_GEOM' must be declared
ORA-06550: line 22, column 6:
PL/SQL: Statement ignored

1 个答案:

答案 0 :(得分:0)

问题是您正在尝试将整个表对象输出到标准输出,而您应该遍历它。而不是:

DBMS_OUTPUT.PUT_LINE(SITE_LIST.SDO_GEOM);

使用类似的东西:

FOR v_i IN 1..SITE_LIST.LAST
LOOP
  dbms_output.put_line(SITE_LIST(v_i).SOME_COLUMN_NAME);
END LOOP;

SOME_COLUMN_NAME替换为光标中所选列的名称。

修改

您必须提供要引用的集合元素的索引:

dbms_output.put_line(site_list(1).SDO_GEOM);

编辑#2

是..您没有为查询中选择的值指定列别名。请尝试使用此光标:

cursor c1 is 
        select 
        SDO_GEOM.SDO_AREA(GEOMETRY, 0.005,'unit = SQ_MILE') AS my_value
          from <tablename>
          where SITE_ID= 8;

然后:

dbms_output.put_line(site_list(1).my_value);