如何在存储过程中创建记录和该记录的表?
Posted
技术标签:
【中文标题】如何在存储过程中创建记录和该记录的表?【英文标题】:How Can I Create Record and Table of That Record Within a Stored Procedure? 【发布时间】:2013-12-25 21:17:47 【问题描述】:在 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】:要修复编译错误,只需删除第二行中的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;
/
(假设您在图中没有循环边或对返回输入顶点的度数不感兴趣)。
【讨论】:
以上是关于如何在存储过程中创建记录和该记录的表?的主要内容,如果未能解决你的问题,请参考以下文章