Thread: Function and RowType

Function and RowType

From
Carlos Roberto Chamorro Mostacilla
Date:
I have a function that receives a Record or a Rowtype,
I need to construct a record of a SQL, to modify a
field of the record and soon to pass it to the
function but when calling the function is generated an
error saying that the variable record is not a column,
find examples of call of funciónes that receive record
but this call becomes in a Query I want to modify the
record before passing it to the function since I do
it?  Excuse my ingles...

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com

Re: Function and RowType

From
Carlos Roberto Chamorro Mostacilla
Date:
Working with 7.4.3

CREATE TABLE a_preuba (
  codigo       varchar(2)           not null,
  descripcion  varchar(250)         not null,
  valor        integer,
  hora         date
);

Function that constructs and returns rowtype:
CREATE OR REPLACE FUNCTION retornar_record(VARCHAR)
RETURNS  a_prueba  AS '
DECLARE
    retorno  a_prueba%rowtype;
    sbestacodi alias for $1;
BEGIN
     SELECT INTO retorno * FROM a_prueba where codigo
= sbestacodi ;
    RETURN retorno;
END;
' LANGUAGE plpgsql;

Function that receives rowtype and writes its content
:
CREATE OR REPLACE FUNCTION probar_record(a_prueba)
RETURNS  varchar AS '
DECLARE
    registro ALIAS FOR $1;
BEGIN
  RAISE NOTICE  '' Código : % Descripción : % Valor  :
 % '',
                 registro.codigo,registro.descripcion,
                 registro.valor;
  RETURN registro.descripcion;
END;
' LANGUAGE plpgsql;


Use of the functions :
1. CREATE OR REPLACE FUNCTION probar()
2.  RETURNS text AS'
3. DECLARE
4.     reg   a_prueba%ROWTYPE;
5. BEGIN
      --To initialize a ROWTYPE
6.    SELECT * INTO reg  FROM retornar_record(''06'');
7.    reg.codigo := ''99'';

        -- It works within a consultation
8.       PERFORM  probar_record(t.*)
                 from a_prueba  t
                  where codigo = ''06'';

       -- It works with a function that returns
rowtype
       --Directly
9.     PERFORM
probar_record(retornar_record(''01''));

       --IT DOES NOT WORK
10.    PERFORM   probar_record(reg);

11.   RETURN NULL;
12. END;
13. ' LANGUAGE plpgsql;

If you observe in line 6 and 7 I initialize rowtype
and I modify it but in 10, line treatment to pass it
to the function and says to me that reg is not a
column;  The calls in line 8 and 9 funciónan, i find
in some forums and probe but I have not been able to
find nothing for line 10, because I need to modify
rowtype before passing it.  Some idea?

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com

Re: Function and RowType

From
Tom Lane
Date:
=?iso-8859-1?q?Carlos=20Roberto=20Chamorro=20Mostacilla?= <carlosrchamorro@yahoo.com> writes:
>        --IT DOES NOT WORK
> 10.    PERFORM   probar_record(reg);

Nope.  plpgsql is pretty limited in what it can do with a rowtype
variable, and one thing it cannot do is pass the entire row into
an SQL expression.

This is fixed in CVS tip, for what that's worth.

            regards, tom lane