2024-09-30 11:10:04
下面是用oracle数据库解决不用start with 来查询子父数据查询方法,里面主要用到了substr 和instr 函数(这两个函数,其他数据库也有相对应的函数),游标(其他数据库也有游标)。
-- 1 前提:创建表以及插入数据
CREATE TABLE TMP_TEST(MAIN_COLUMN VARCHAR2(10),PARENT_COLUMN VARCHAR2(10));
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('A',NULL);
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('B','A');
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('C','A');
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('D','A');
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('E','B');
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('F','C');
INSERT INTO TMP_TEST(MAIN_COLUMN,PARENT_COLUMN) VALUES('G','E');
-- 2 创建存储过程
CREATE OR REPLACE PROCEDURE GET_TREE(IS_PARENT IN NUMBER /** 子父查询 **/,
SEARCH_ID IN VARCHAR2 /** 查询条件节点 **/,
TREE_RESOUT OUT VARCHAR2 /** 输出结果集合 **/)
AS
V_TEMP VARCHAR2(4000);
V_SEARCH VARCHAR2(4000);
V_INDEX INTEGER;
BEGIN
V_TEMP :=SEARCH_ID||'-';
TREE_RESOUT := '';
WHILE length(V_TEMP) > 0 LOOP
V_INDEX := instr(V_TEMP,'-');
V_SEARCH := substr(V_TEMP,0,V_INDEX-1);
V_TEMP := substr(V_TEMP,V_INDEX+1);
/*DBMS_OUTPUT.put_line('V_INDEX:'|| V_INDEX ||'V_TEMP:' ||V_TEMP||'V_SEARCH:'|| V_SEARCH);*/
/** 查询子节点 **/
if(IS_PARENT = 1) THEN
FOR C1 IN (SELECT * FROM TMP_TEST T1 WHERE T1.PARENT_COLUMN = V_SEARCH) LOOP
TREE_RESOUT := TREE_RESOUT || C1.MAIN_COLUMN;
V_TEMP := V_TEMP || C1.MAIN_COLUMN || '-';
END LOOP;
ELSE
/** 查询父节点 **/
FOR C1 IN (SELECT * FROM TMP_TEST T1 WHERE T1.MAIN_COLUMN = V_SEARCH) LOOP
TREE_RESOUT := TREE_RESOUT || C1.PARENT_COLUMN;
V_TEMP := V_TEMP || C1.PARENT_COLUMN || '-';
END LOOP;
END IF;
END LOOP;
/*DBMS_OUTPUT.put_line('TREE_RESOUT:'||TREE_RESOUT);*/
END;
-- 3 调用存储过程
declare
TREE_RESULT VARCHAR2(4000);
SEARCH_ID VARCHAR2(4000);
begin
get_tree(1,'A',TREE_RESULT);
dbms_output.put_line('查询子节点:' || TREE_RESULT);
get_tree(0,'G',TREE_RESULT);
dbms_output.put_line('查询父节点:' || TREE_RESULT);
end;