Question

In Oracle 10g I have tried this but having problem with compilation. I can't understand where the problem is. Please help...

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;    
/    

Warning: Procedure created with compilation errors.....

Était-ce utile?

La solution

To fix the compile error then just remove the DECLARE statement in the second line.

However, you can dramatically simplify the code:

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;
/

(Assuming you don't have looping edges in the graph or aren't interested in returning the degree of the input vertex).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top