assign the row count of a query to a variable

From: "Kevin B(dot)" <db(at)ke5in(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: assign the row count of a query to a variable
Date: 2005-01-14 20:43:04
Message-ID: 1386.67.87.30.165.1105735384.squirrel@www.ke5in.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I'm trying to assign the row count of a query to a variable in a function
but I'm not having any luck.

Could someone tell me the syntax? I've been looking in the docs and
googling for a long time but just can't find the answer.

I've tried:
CREATE OR REPLACE FUNCTION ret1() RETURNS int4 AS '
BEGIN
declare
var int4;
begin
--select var count(*) from T;
--select var (count(*)) from T;
--select var = count(*) from T;
var = select count(*) from T;
return var;
END;
END;
'
LANGUAGE 'plpgsql';

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message PFC 2005-01-14 21:13:57 Re: assign the row count of a query to a variable
Previous Message KÖPFERL Robert 2005-01-14 18:44:06 Re: Column with recycled sequence value (solved (somehow))