如何在存储过程中创建记录和记录表?

时间:2013-12-25 21:17:48

标签: database oracle plsql oracle10g plsqldeveloper

在Oracle 10g中,我尝试了这个但是编译时遇到了问题。我无法理解问题出在哪里。请帮忙......

create or replace procedure get_degree(ver char) as   
declare    
  type edge_data is record    
  (    
     vertex1 varchar2(10),    
     vertex2 varchar2(10)    
  );    
  ed edge_data;    
  type e_d_t is table of edge_data index by pls_integer;    
  edt e_d_t;    
  n integer;    
  deg integer;     
begin    
  select max(rn) into n 
  from (  select rownum rn,vertex1,vertex2 from edges 
          where vertex1=ver or vertex2=ver
  );    
  for i in 1..n loop     
     select vertex1,vertex2 into ed 
     from (  select rownum rn,vertex1,vertex2 from edges 
             where vertex1=ver or vertex2=ver
     ) 
     where rn=i;    
     edt(i):=ed;    
     if edt(i).vertex1=ver then    
        select degree into deg from vertices 
        where ver_name=edt(i).vertex2;    
        dbms_output.put_line(edt(i).vertex2||'='||deg);    
     else    
        select degree into deg from vertices 
        where ver_name=edt(i).vertex1;    
        dbms_output.put_line(edt(i).vertex1||'='||deg);    
     end if;    
  end loop;    
end;    
/    

警告:使用编译错误创建的过程.....

1 个答案:

答案 0 :(得分:0)

要修复编译错误,只需删除第二行中的DECLARE语句。

但是,您可以大大简化代码:

CREATE OR REPLACE PROCEDURE get_degree(ver CHAR)
AS
  TYPE vertex_data IS RECORD    
  (    
     ver_name Vertices.Ver_Name%TYPE,    
     degree   Vertices.Degree%TYPE    
  );    
  TYPE vertex_data_table IS TABLE OF vertex_data;    
  vdt vertex_data_table;
BEGIN
  SELECT ver_name,degree 
  BULK COLLECT INTO vdt
  FROM   vertices v
  WHERE  ver_name <> ver
  AND    EXISTS ( SELECT 'X'
                  FROM   Edges e
                  WHERE  ( e.Vertex1 = ver AND e.Vertex2 = v.Ver_Name )
                  OR     ( e.Vertex2 = ver AND e.Vertex1 = v.Ver_Name )
                );

  FOR i IN 1..vdt.COUNT LOOP 
    dbms_output.put_line(vdt(i).ver_name||'='||vdt(i).degree);
  END LOOP;
END get_degree;
/

(假设图中没有循环边或者对返回输入顶点的程度不感兴趣)。