views:

1117

answers:

3

I'd like to run a query like the one below against an Oracle 9i database from Java (an example table structure and example data are below).

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;

This works fine. However, if the sys_connect_by_path which builds up comma_list hits the varchar2 4000 character limit, then I get an "ORA-01489: result of string concatenation is too long" error.

Does anyone have any suggestions on how to overcome this limit so my concatenation can exceed 4000 characters?

Tables and example data:


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);
+1  A: 

Is it necessary to build the comma-list in SQL?

Since you are running this from Java anyway, maybe you could query rows with "parent_id" ,"child_id", "tree_level" columns and build the employee path in your application code ? I suppose you are splitting it into a List now anyway (a 4000 character string cannot be for direct display purposes).

Thilo
I need to concatenate everything together to be sent on a file based interface. Like you say, I can pull multiple rows instead, iterate over those in Java and concatenate. But I wondered if I could do it all in one hit to the DB.
A_M
I do not think it makes sense performance-wise. You would also be sending much more data than necessary over the wire. But it is an interesting exercise. Google "STRAGG" or "sys_connect_by_path" for several approaches to the problem. It can be made to work with CLOB, I believe.
Thilo
A: 

Two ideas:

  • do not concatenate the actual strings, but shorter placeholders and use some replace method (in java or otherwise) to insert the real names

  • you could create multiple columns: comma_list_ where each holds the n-th group of 400 employees. The final value would result from concatinating these columns in the client.

Jens Schauder
A: 

I was understanding that the 4000 character limit for varchar2 was only for columns -- that varchar2 lengths were actually allowed to go MUCH larger, for example in PL/SQL. Are you sure that your query is not recursing incorrectly and generating some large cartesian product?

Metro