这是一个 PL/SQL 函数,我遇到的问题是,当它返回一个大字符串时,它会生成以下消息:
选择 TODAS_NOTIFIACIONES_ACTIVAS('21/10/2023', 0, 10) 作为结果 FROM DUAL 命令行错误:1 列:8 错误报告 - SQL 错误: ORA-01489: 字符串连接的结果太长 ORA-06512: at “NOTIFICACIONES_PKG”,第 95 行
00000 - “字符串连接的结果太长” *原因:字符串拼接结果超过最大长度。 *操作:确保结果小于最大尺寸
这是功能代码:
FUNCTION TODAS_NOTIFICACIONES_ACTIVAS(fecha_val IN VARCHAR2, start_position IN NUMBER, end_position IN NUMBER)
RETURN CLOB
AS
l_result1 CLOB;
l_result2 CLOB;
l_result3 CLOB;
l_count NUMBER;
BEGIN
SELECT COUNT(*) INTO l_count
FROM NOTIFICATIONS
WHERE TO_DATE(fecha_val, 'DD/MM/YYYY') >= NOTIFICATION_DATE_FROM
AND TO_DATE(fecha_val, 'DD/MM/YYYY') <= NOTIFICATION_DATE_TO;
l_result1 := '{ "count": ' || TO_CHAR(l_count) || ', "data": ';
SELECT '[' ||
LISTAGG(
'{ "ID": "' || ID || '", "RECIPIENTS": "' || RECIPIENTS || '", "AGE_FROM": "' || AGE_FROM || '", "AGE_TO": "' || AGE_TO || '", "DEPARTMENT": "' || DEPARTMENT_ID || '", "LOCALITY": "' || LOCALITY_ID || '", "MESSAGE_TITLE": "' || MESSAGE_TITLE || '", "MESSAGE_BODY": "' || MESSAGE_BODY || '", "MULTIMEDIA_ID": "' || MULTIMEDIA_ID || '", "NOTIFICATION_DATE_FROM": "' || TO_CHAR(NOTIFICATION_DATE_FROM, 'DD/MM/YYYY HH24:MI:SS') || '", "NOTIFICATION_DATE_TO": "' || TO_CHAR(NOTIFICATION_DATE_TO, 'DD/MM/YYYY HH24:MI:SS') || '", "SEND_BY_EMAIL": "' || SEND_BY_EMAIL || '", "CREATED_AT": "' || TO_CHAR(CREATED_AT, 'DD/MM/YYYY HH24:MI:SS') || '", "DELETED_AT": "' || TO_CHAR(DELETED_AT, 'DD/MM/YYYY HH24:MI:SS') || '"}',
', '
) WITHIN GROUP (ORDER BY CREATED_AT DESC) ||
']' INTO l_result2
FROM (
SELECT
ID, RECIPIENTS, AGE_FROM, AGE_TO, DEPARTMENT_ID, LOCALITY_ID,
MESSAGE_TITLE, MESSAGE_BODY, MULTIMEDIA_ID,
NOTIFICATION_DATE_FROM, NOTIFICATION_DATE_TO, SEND_BY_EMAIL,
CREATED_AT, DELETED_AT,
ROW_NUMBER() OVER (ORDER BY CREATED_AT DESC) AS rn
FROM NOTIFICATIONS
WHERE TO_DATE(fecha_val, 'DD/MM/YYYY') >= NOTIFICATION_DATE_FROM
AND TO_DATE(fecha_val, 'DD/MM/YYYY') <= NOTIFICATION_DATE_TO
)
WHERE rn BETWEEN start_position AND end_position;
l_result3 := l_result1 ||l_result2 || '}';
RETURN l_result3;
END;
尝试修复它,我尝试了使用 start_position 和 end_position 的“分页”方法,但我不知道哪一列号不会返回该错误。例如,对于 10 行,它工作正常,直到出现错误新的通知消息。还有其他方法可以修复它吗?退货前有什么需要牢记的注意事项或初步检查吗?
如果您想在 Oracle 中创建 JSON,那么从 Oracle 12 开始,请使用 JSON 函数,并且不要尝试手工制作 JSON 字符串(对于初学者来说,当字符串太长时,它会失败,但您也不会处理任何转义字符可能会出现在字符串中)。
类似这样的:
CREATE FUNCTION TODAS_NOTIFICACIONES_ACTIVAS(
fecha_val IN VARCHAR2,
start_position IN NUMBER,
end_position IN NUMBER
) RETURN CLOB
AS
l_json CLOB;
BEGIN
SELECT JSON_OBJECT(
KEY 'count' VALUE COUNT(*),
KEY 'data' VALUE JSON_ARRAYAGG(
JSON_OBJECT(
KEY 'ID' VALUE id,
KEY 'RECIPIENTS' VALUE recipients,
KEY 'AGE_FROM' VALUE age_from,
KEY 'AGE_TO' VALUE age_to,
KEY 'DEPARTMENT' VALUE department_id,
KEY 'LOCALITY' VALUE locality_id,
KEY 'MESSAGE_TITLE' VALUE message_title,
KEY 'MESSAGE_BODY' VALUE message_body,
KEY 'MULTIMEDIA_ID' VALUE multimedia_id,
KEY 'NOTIFICATION_DATE_FROM' VALUE TO_CHAR(notification_date_from, 'DD/MM/YYYY HH24:MI:SS'),
KEY 'NOTIFICATION_DATE_TO' VALUE TO_CHAR(notification_date_to, 'DD/MM/YYYY HH24:MI:SS'),
KEY 'SEND_BY_EMAIL' VALUE send_by_email,
KEY 'CREATED_AT' VALUE TO_CHAR(created_at, 'DD/MM/YYYY HH24:MI:SS'),
KEY 'DELETED_AT' VALUE TO_CHAR(deleted_at, 'DD/MM/YYYY HH24:MI:SS')
RETURNING CLOB
)
RETURNING CLOB
)
RETURNING CLOB
)
INTO l_json
FROM NOTIFICATIONS
WHERE TO_DATE(fecha_val, 'DD/MM/YYYY') >= NOTIFICATION_DATE_FROM
AND TO_DATE(fecha_val, 'DD/MM/YYYY') <= NOTIFICATION_DATE_TO;
RETURN l_json;
END;
/
(如果需要,请重新应用分页。)