1

I am using postgresql, I would like to know how to send a parameter as a table in a function.

I have already created the type type_detalle_ac:

CREATE TYPE public.type_detalle_ac AS
(
 id_componente integer,
 precio numeric(10,2),
 cantidad integer,
 sub_total numeric(10,2)
);

and I use it in the following function:

CREATE OR REPLACE FUNCTION public.sp_adm_artefacto(
v_serie character varying DEFAULT NULL::character varying,
v_tipo_artefacto integer DEFAULT NULL::integer,
v_modelo character varying DEFAULT NULL::character varying,
v_marca integer DEFAULT NULL::integer,
v_detalle type_detalle_ac DEFAULT NULL::type_detalle_ac,
v_usuario integer DEFAULT NULL::integer,
v_id_artefacto integer DEFAULT NULL::integer,
v_tipo_operacion character DEFAULT NULL::bpchar)
RETURNS void
LANGUAGE 'plpgsql'

COST 100
VOLATILE 
ROWS 0
AS $BODY$

DECLARE 

    VL_ID_ARTEFACTO INTEGER;
BEGIN
    IF V_TIPO_OPERACION = 'I' THEN

        INSERT INTO DETALLE_AC(ID_DETALLE_AC,ID_ARTEFACTO,ID_COMPONENTE,PRECIO,CANTIDAD,SUB_TOTAL,
                               USUARIO_CREACION,FECHA_CREACION)
        SELECT 1,1,ID_COMPONENTE,PRECIO,CANTIDAD,SUB_TOTAL,v_usuario,NOW() FROM V_DETALLE;

    END IF;

END;


$BODY$;

But when calling the function:

SELECT "sp_adm_artefacto"('1321321',1,'F-14',1,(1,10,5,50),1,NULL,'I')

I get an error :

ERROR:  no existe la relación «v_detalle»
LINE 3: ...NTE,PRECIO,CANTIDAD,SUB_TOTAL,v_usuario,NOW() FROM V_DETALLE

Thank you

4
  • It seems the errors says the v_detalle table doesn't exist and it's not about the type. Do you have such table? Commented Mar 2, 2018 at 19:08
  • hi Abelisto, I already added more information Commented Mar 2, 2018 at 19:14
  • hello kaveh, v_detalle is the type_detalle_ac parameter Commented Mar 2, 2018 at 19:18
  • I worked, thanks Abelisto Commented Mar 2, 2018 at 23:51

2 Answers 2

1

v_detalle is not a table, so you cannot use it in the FROM clause of a query.

You should write

INSERT INTO detalle_ac (...)
VALUES (1, 1, v_detalle.id_componente, v_detalle.precio, ...);
Sign up to request clarification or add additional context in comments.

Comments

0

There are five sources of data in the FROM clause:

  • Table
  • View
  • Values (select * from (values(1,'a'),(2,'b')) as t(x,y);)
  • Set returning functions (link)
  • Subqueries

For your question: Laurenz already answered about values way

Yet another way, shorter and more general but probably slower a bit:

select * from (select variable.*) as t;

It transforms your variable to table-like subquery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.