I have a database and within it I have the function create_functions and within the function create_functions I want to create another function transaccioncuentas to be able to create it in another database as it looks but it marks me an error in the DECLARE of the transaccioncuentas function. Does anyone know if this can be done?
CREATE OR REPLACE FUNCTION public.crear_funciones(
rfc character varying)
RETURNS character varying
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
comando VARCHAR:=('CREATE FUNCTION');
BEGIN
/*RETURN 'HOLA MUNDO';*/
PERFORM * from dblink('dbname='||rfc||'','
CREATE OR REPLACE FUNCTION public.transaccioncuentas(
_post character varying[])
RETURNS integer
LANGUAGE ''plpgsql''
AS $BODY$
DECLARE
_fecha timestamp;
_ejercicio INTEGER;
_periodo INTEGER;
_tipopol INTEGER;
_cuendestino character varying(32);
_corigen character varying(32);
_cdestino character varying(32);
_referencia character varying(100);
_deposito DECIMAL (12,2);
_porigen INTEGER;
_pdestino INTEGER;
_maxfolio INTEGER;
idultimapoliza INTEGER;
BEGIN
_fecha = _post[1];
_ejercicio = _post[2];
_periodo = _post[3];
_tipopol = _post[4];
_cuendestino = _post[5];
_corigen = _post[6];
_cdestino = _post[7];
_referencia = _post[8];
_deposito = _post[9];
_porigen = _post[10];
_pdestino = _post[11];
_maxfolio := (SELECT getmaxfolio(_periodo,_tipopol,_ejercicio));
_maxfolio = _maxfolio + 1;
INSERT INTO polizas(ejercicio,folio,periodo,tipopol,concepto,fecha,cargos,abonos,status,status2,referencia) values(_ejercicio,_maxfolio,_periodo,_tipopol,_cuendestino || ' ' || _referencia,_fecha,_deposito,_deposito,1,1,_referencia)
RETURNING id INTO idultimapoliza;
INSERT INTO detalle_polizas(idpoliza,ejercicio,periodo,tipopol,nummovto,idcuenta,tipomovto,importe,concepto,referencia,fecha,idsegneg,id_padre,status2)
VALUES(idultimapoliza,_ejercicio,_periodo,3,1,_corigen,0,_deposito,_cuendestino,_referencia,_fecha,1,_porigen,1);
INSERT INTO detalle_polizas(idpoliza,ejercicio,periodo,tipopol,nummovto,idcuenta,tipomovto,importe,concepto,referencia,fecha,idsegneg,id_padre,status2)
VALUES(idultimapoliza,_ejercicio,_periodo,3,1,_cdestino,1,_deposito,_cuendestino,_referencia,_fecha,1,_pdestino,1);
return idultimapoliza;
END;
DELIMITER ;
$BODY$; ') as db(crear_funciones character varying);
RETURN comando;
END;
$BODY$;