Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using %rowtype when returning in a PostgreSQL function

If I have a function that returns only one row with some columns from a table do I need to add %rowtype in the function return declaration?

CREATE OR REPLACE FUNCTION test(int n)
RETURNS tableName%rowtype AS
$BODY$
DECLARE 
    r tableName%rowtype;    
BEGIN   
        select a,b,c into r from tableName where d=n;
        return r;
$BODY$
END;
like image 996
Douglas Avatar asked Apr 25 '14 15:04

Douglas


People also ask

How do I return a set of records in PostgreSQL?

Let's make a function that returns all the rows of a table whose name you pass in as a parameter. create or replace function GetRows(text) returns setof record as ' declare r record; begin for r in EXECUTE ''select * from '' || $1 loop return next r; end loop; return; end ' language 'plpgsql';

Can a PostgreSQL function return a table?

PostgreSQL returns a table with one column that holds the array of films. In practice, you often process each individual row before appending it in the function's result set. The following example illustrates the idea.

Can PostgreSQL stored procedure return resultset?

You can call a PostgreSQL stored procedure and process a result set in a . NET application, for example, in C# application using Npgsql . NET data provider. Note that you do not need to know the name of the cursor to process the result set.

What does $$ mean in PostgreSQL?

A dollar sign ($) followed by digits is used to represent a positional parameter in the body of a function definition or a prepared statement. In other contexts the dollar sign may be part of an identifier or a dollar-quoted string constant.


1 Answers

About %ROWTYPE

The %ROWTYPE construct is only good for portability. Rarely useful, since PL/pgSQL functions are hardly portable to begin with.
If you are going to use it, it's only meant for variable declaration inside PL/pgSQL function, not to declare the RETURN type, which is actually part of the outer SQL syntax.

Per documentation:

(Since every table has an associated composite type of the same name, it actually does not matter in PostgreSQL whether you write %ROWTYPE or not. But the form with %ROWTYPE is more portable.)

Answer

This would achieve what you seem to be trying:

CREATE OR REPLACE FUNCTION test_plpgsql(_n int)
  RETURNS tbl AS
$func$
BEGIN   
  RETURN (SELECT t FROM tbl t where tbl_id = _n); -- selecting the whole row
END
$func$ LANGUAGE plpgsql;

Call:

SELECT * FROM test_plpgsql(1);

But if it's as simple as that, use a simpler SQL function instead:

CREATE OR REPLACE FUNCTION test_sql(_n int)
  RETURNS SETOF tbl AS
$func$
   SELECT * FROM tbl WHERE tbl_id = _n;  -- Requires Postgres 9.3; or use $1
$func$ LANGUAGE sql;

Call:

SELECT * FROM test_sql(1);

Your original example was twisted and incorrect in too many places. Search for more plpgsql examples to get a grasp on basic syntax.

like image 167
Erwin Brandstetter Avatar answered Nov 10 '22 18:11

Erwin Brandstetter