我的想法是,用户任意输入一个结点传递给@NODE,然后建立一张临时表,将以这个节点为父节点的所有结点都保存到临时表中,这样不断循环,知道查找结果为空时,将该层的深度@DEPTH返回。
可是在运行中对@NODE报错,并且在命令中执行exec get_treedepth(@node)也没有返回结果,我是新手,请大家帮忙看看哪里有问题啊,在PL/SQL中运行的,ORACLE 10G。谢谢大家了
create or replace procedure Get_TreeDepth(
@Node NUMBER(10) input,
@Depth OUT number(10))
as
begin
--建立临时表
set @sql_exec = create global temporary table NodeCal
(id number(10),
levelno number(10),
QTY number(10),
ParentID number(10)
)
exec (@sql_exec)
set @depth = select levelno from msot_nodetest where id = @Node
--初始化
set @sql_initial = insert into NodeCal values(select * from msot_nodetest where id = @node)
exec (@sql_initial)
--开始逐层遍历
while (exists(select * from msot_nodetest where parentID in (select id from NodeCal))) loop
set @sql_insert = insert into NodeCal values(select * from msot_nodetest where parentid in (select id from NodeCal))
exec (@sql_insert)
@Depth = @Depth + 1
end loop
DBMS_OUTPUT.PUT_LINE(@Depth-1)
------解决方案--------------------
你这个是mssql语法吧,先给你改改语法
- SQL code
CREATE OR REPLACE PROCEDURE Get_TreeDepth(Node NUMBER(10) input, Depth OUT NUMBER(10)) AS sql_exec VARCHAR2(2000);BEGIN --建立临时表 exec IMMEDIATE 'drop table NodeCal'; sql_exec = 'create global temporary table NodeCal (id number(10), levelno number(10), QTY number(10), ParentID number(10) ) on commit '; exec IMMEDIATE sql_exec; SELECT levelno INTO depth FROM msot_nodetest WHERE id = Node; INSERT INTO NodeCal SELECT * FROM msot_nodetest WHERE id = node; --开始逐层遍历 FOR c IN (SELECT * FROM msot_nodetest WHERE parentID IN (SELECT id FROM NodeCal)) LOOP INSERT INTO NodeCal SELECT * FROM msot_nodetest WHERE parentid IN (SELECT id FROM NodeCal); Depth = Depth + 1; END LOOP DBMS_OUTPUT.PUT_LINE(Depth - 1);END;
------解决方案--------------------
------解决方案--------------------
sql_exec := 'create global temporary table NodeCal
(id number(10),
levelno number(10),
QTY number(10),
ParentID number(10)
) on commit ';
oracle 中赋值是这样的a:=1 的格式
------解决方案--------------------