I have the following problem, I am creating a Trigger in Oracle so that I write information in a txt file, up to here everything is fine, I can write without dramas, the problem is when I need to add information to the file, apart from the one that already has . I understand that I must read the file, extract what it contains and put it together with the new text, the big problem arises when I try to open the file but it does not exist. How can I add this validation (if it does not exist, what do you think)?
This is the Trigger:
CREATE OR REPLACE TRIGGER BAJOSTOCK
AFTER
UPDATE
ON PRODUCTO
FOR EACH ROW
DECLARE
V_ARCHIVO UTL_FILE.FILE_TYPE;
V_LINEA VARCHAR2(1024);
BEGIN
IF :NEW.CANTIDAD <= :OLD.STOCK_CRITICO THEN
--Aqui me da el errror, cuando no existe
--Abro el archivo
V_ARCHIVO := UTL_FILE.FOPEN ('DIR_TMP', 'stock.txt', 'r');
--Leo lo que contiene
LOOP
UTL_FILE.GET_LINE (V_ARCHIVO, V_LINEA);
END LOOP;
UTL_FILE.FCLOSE(V_ARCHIVO);--Lo cierro
--Vuelvo a abrirlo y escribo
V_ARCHIVO := UTL_FILE.FOPEN ('DIR_TMP', 'stock.txt', 'w');
--Escribo lo que tenía y el nuevo contenido
UTL_FILE.PUT_LINE(V_ARCHIVO,V_LINEA);
UTL_FILE.PUT_LINE (V_ARCHIVO, 'El producto '|| :OLD.DESCRIPCION ||' se encuentra en stock crítico, por favor revíselo');
UTL_FILE.FCLOSE(V_ARCHIVO); --Cierro el archivo
END IF;
--Cierro el archivo si hay errores
EXCEPTION
WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(V_ARCHIVO);
END;
/
I appreciate your help ....