I'm trying to execute the below query, which pivots a table by passing the values to pivot in a variable. It has worked for other queries.
DECLARE
cols VARCHAR2(30000);
BEGIN
SELECT
LISTAGG(''''
|| agr_name
|| ''' as "'
|| agr_name
|| '"', ',') WITHIN GROUP(
ORDER BY
agr_name
)
INTO cols
FROM
(
SELECT DISTINCT
to_char(agr_name) AS agr_name
FROM
dat_skills
-- WHERE
-- ROWNUM < 400
);
EXECUTE IMMEDIATE q'[
CREATE OR REPLACE VIEW vw_dat_skills AS
SELECT
*
FROM
dat_skills PIVOT (
COUNT ( agr_name )
FOR agr_name
IN (]'||cols||q'[)
)
]'
;
END;
But I'm getting the below error:
ORA-01489: result of string concatenation is too long
01489. 00000 - "result of string concatenation is too long"
*Cause: String concatenation result is more than the maximum size.
*Action: Make sure that the result is less than the maximum size.
If I execute the select distinct it retrieves 138 rows with a total length of 2777 characters of length.
WITH aux AS (
SELECT DISTINCT
to_char(agr_name) AS agr_name
FROM
dat_skills
)
SELECT
count(1), sum(length(agr_name))
FROM
aux
Is there any workaround I could take to bypass this restriction? Or something that I'm doing wrong?