1
votes

I have this problem when I try to execute the following code:

CREATE OR REPLACE FUNCTION USP_IP_CREA_EMPRESA_CATEGORIA
(p_nombre td_nombre_extenso
,p_codigoEmpresa td_codigo10
,p_codigoPadre int
,p_orden int
,p_muestraBoletin td_estado_auditoria
,p_categoriaDefecto td_estado_auditoria
,p_envioAutomatico td_estado_auditoria
,p_envioDirecto td_estado_auditoria
,p_usuario td_usuario_auditoria
,p_direccionIp td_ip_auditoria
) RETURNS VOID
--WITH ENCRYPTION
AS $$
    DECLARE v_ordenReal INT = (SELECT (max(num_orden) + 1) FROM IP_RE_EMPRESA_CATEGORIA WHERE cod_empresa = p_codigoEmpresa);
 v_ordenHijo INT = (SELECT (max(num_orden) + 1) FROM IP_RE_EMPRESA_CATEGORIA WHERE cod_empresa = p_codigoEmpresa and (id_padre = p_codigoPadre));
BEGIN
IF p_orden = 0 and p_codigoPadre = 0
THEN
    p_orden := v_ordenReal;
ELSE
    p_orden := v_ordenHijo;
END IF;

DECLARE NuevoIdentity TABLE (id INT)
INSERT INTO IP_RE_EMPRESA_CATEGORIA
(
    des_nombre
    , cod_empresa
    , id_padre
    , num_orden
    , est_mostrar_boletin
    , est_categoria_defecto
    , est_envio_automatico
    , est_envio_directo
    , cod_usuario_registro
    , des_ip_registro
)
OUTPUT INSERTED.id_categoria INTO NuevoIdentity(id)
SELECT
(
    p_nombre
    , p_codigoEmpresa
    , p_codigoPadre
    , COALESCE(p_orden, 0)
    , p_muestraBoletin
    , p_categoriaDefecto
    , p_envioAutomatico
    , p_envioDirecto
    , p_usuario
    , p_direccionIp
)

SELECT id FROM NuevoIdentity;
END;

$$ LANGUAGE plpgsql;

Finally the version 9.6 of Postgresql give me this error:

ERROR:  syntax error at or near "TABLE"
LINE 27:  DECLARE NuevoIdentity TABLE (id INT)
                                ^
CONTEXT:  invalid type name "TABLE (id INT)
    INSERT INTO IP_RE_EMPRESA_CATEGORIA
    (
        des_nombre
        , cod_empresa
        , id_padre
        , num_orden
        , est_mostrar_boletin
        , est_categoria_defecto
        , est_envio_automatico
        , est_envio_directo
        , cod_usuario_registro
        , des_ip_registro
    )
    OUTPUT INSERTED.id_categoria INTO NuevoIdentity(id)
    SELECT
    (
        p_nombre
        , p_codigoEmpresa
        , p_codigoPadre
        , COALESCE(p_orden, 0)
        , p_muestraBoletin
        , p_categoriaDefecto
        , p_envioAutomatico
        , p_envioDirecto
        , p_usuario
        , p_direccionIp
    )

    SELECT id FROM NuevoIdentity"
********** Error **********

ERROR: syntax error at or near "TABLE"
SQL state: 42601
Character: 814
Context: invalid type name "TABLE (id INT)
    INSERT INTO IP_RE_EMPRESA_CATEGORIA
    (
        des_nombre
        , cod_empresa
        , id_padre
        , num_orden
        , est_mostrar_boletin
        , est_categoria_defecto
        , est_envio_automatico
        , est_envio_directo
        , cod_usuario_registro
        , des_ip_registro
    )
    OUTPUT INSERTED.id_categoria INTO NuevoIdentity(id)
    SELECT
    (
        p_nombre
        , p_codigoEmpresa
        , p_codigoPadre
        , COALESCE(p_orden, 0)
        , p_muestraBoletin
        , p_categoriaDefecto
        , p_envioAutomatico
        , p_envioDirecto
        , p_usuario
        , p_direccionIp
    )

    SELECT id FROM NuevoIdentity"
2

2 Answers

0
votes

There's no such thing as a TABLE variable in Postgres. You can create a temp table:

https://www.postgresql.org/docs/current/static/sql-createtable.html

-1
votes

Postgres can be a headache to work with , specially if you are using the default phpPgAdmin. While running Update , Insert or Alter commands , uncheck the Paginate results check box beneath the query box, and enjoy this trick.