我想从Java运行一个类似下面的查询来反对Oracle 9i数据库(示例表结构和示例数据如下)。
SELECT deptno , SUBSTR(comma_list, 2) comma_list FROM (SELECT deptno , SYS_CONNECT_BY_PATH(ename, ',') comma_list , row_number , row_count FROM (SELECT deptno , ename , ROW_NUMBER()OVER(PARTITION BY deptno ORDER BY empno) row_number , COUNT(*)OVER(PARTITION BY deptno) row_count FROM wd_emp) START WITH row_number = 1 CONNECT BY deptno = PRIOR deptno AND row_number = PRIOR row_number + 1) WHERE row_number = row_count;
这很好用。但是,如果构建comma_list的sys_connect_by_path达到varchar2 4000字符限制,那么我得到“ORA-01489:字符串连接的结果太长”错误。
有没有人对如何克服这个限制有任何建议,所以我的串联可以超过4000个字符?
表格和示例数据:
CREATE TABLE WD_DEPT(DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY ,DNAME VARCHAR2(14) ,LOC VARCHAR2(13)); CREATE TABLE WD_EMP(EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY ,ENAME VARCHAR2(10) ,JOB VARCHAR2(10) ,MGR NUMBER(4) ,HIREDATE DATE ,SAL NUMBER(7,2) ,DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES WD_DEPT); INSERT INTO WD_DEPT VALUES(10,'TEAM GREGORY','TABLE 3'); INSERT INTO WD_DEPT VALUES(20,'TEAM HANLEY','TABLE 2'); INSERT INTO WD_DEPT VALUES(30,'TEAM OFFIAH','TABLE 4'); INSERT INTO WD_DEPT VALUES(40,'TEAM BOTICA','TABLE 1'); INSERT INTO WD_DEPT VALUES(50,'TEAM SKERRETT','TABLE 4'); INSERT INTO WD_DEPT VALUES(60,'TEAM McGINTY','TABLE 1'); INSERT INTO WD_DEPT VALUES(70,'EMPTY TEAM','NO TABLE'); INSERT INTO WD_EMP VALUES(11,'GREGORY', 'TEAM LEAD', 28, to_date('18-JAN-2000', 'DD-MON-RRRR'), 800, 10); INSERT INTO WD_EMP VALUES(12,'BELL', 'DEVELOPER', 11, to_date('17-JAN-2000', 'DD-MON-RRRR'), 600, 10); INSERT INTO WD_EMP VALUES(13,'CLARKE', 'DEVELOPER', 11, to_date('16-JAN-2000', 'DD-MON-RRRR'), 600, 10); INSERT INTO WD_EMP VALUES(14,'HANLEY', 'TEAM LEAD', 28, to_date('15-JAN-2000', 'DD-MON-RRRR'), 800, 20); INSERT INTO WD_EMP VALUES(15,'BETTS', 'CONTRACTOR', 14, to_date('14-JAN-2000', 'DD-MON-RRRR'), 700, 20); INSERT INTO WD_EMP VALUES(16,'MILES', 'CONTRACTOR', 14, to_date('13-JAN-2000', 'DD-MON-RRRR'), 700, 20); INSERT INTO WD_EMP VALUES(17,'HAMPSON', 'DEVELOPER', 14, to_date('12-JAN-2000', 'DD-MON-RRRR'), 600, 20); INSERT INTO WD_EMP VALUES(18,'PRESTON', 'DEVELOPER', 14, to_date('11-JAN-2000', 'DD-MON-RRRR'), 600, 20); INSERT INTO WD_EMP VALUES(19,'OFFIAH', 'TEAM LEAD', 28, to_date('10-JAN-2000', 'DD-MON-RRRR'), 800, 30); INSERT INTO WD_EMP VALUES(20,'PLATT', 'DEVELOPER', 19, to_date('09-JAN-2000', 'DD-MON-RRRR'), 600, 30); INSERT INTO WD_EMP VALUES(21,'POTTER', 'DEVELOPER', 19, to_date('08-JAN-2000', 'DD-MON-RRRR'), 600, 30); INSERT INTO WD_EMP VALUES(22,'CASE', 'DEVELOPER', 19, to_date('07-JAN-2000', 'DD-MON-RRRR'), 600, 30); INSERT INTO WD_EMP VALUES(23,'BOTICA', 'TEAM LEAD', 28, to_date('06-JAN-2000', 'DD-MON-RRRR'), 800, 40); INSERT INTO WD_EMP VALUES(24,'GILL', 'DEVELOPER', 23, to_date('05-JAN-2000', 'DD-MON-RRRR'), 600, 40); INSERT INTO WD_EMP VALUES(25,'SKERRETT', 'TEAM LEAD', 28, to_date('04-JAN-2000', 'DD-MON-RRRR'), 800, 50); INSERT INTO WD_EMP VALUES(26,'McGINTY', 'TEAM LEAD', 28, to_date('03-JAN-2000', 'DD-MON-RRRR'), 800, 60); INSERT INTO WD_EMP VALUES(27,'LOWE', 'MANAGER', 28, to_date('02-JAN-2000', 'DD-MON-RRRR'), 900, NULL); INSERT INTO WD_EMP VALUES(28,'MONIE', 'MANAGER', NULL, to_date('01-JAN-2000', 'DD-MON-RRRR'), 1000, NULL);
答案 0 :(得分:2)
是否有必要在SQL中构建逗号列表?
既然你是从Java运行它,也许你可以使用“parent_id”,“child_id”,“tree_level”列查询行并在应用程序代码中构建员工路径?我想你现在正在把它分成一个列表(一个4000字符的字符串不能用于直接显示)。
答案 1 :(得分:0)
两个想法:
不要连接实际的字符串,而是使用更短的占位符并使用一些替换方法(在java或其他方面)插入真实姓名
您可以创建多个列:comma_list_其中每个列包含第n组400名员工。最终值将来自在客户端中汇总这些列。
答案 2 :(得分:0)
我理解varchar2的4000个字符限制仅适用于列 - 实际上允许varchar2长度更大,例如在PL / SQL中。您确定您的查询没有错误地递归并生成一些大型笛卡尔积吗?