knowledge-database (beta)

Current group: pgsql.sql

assign the row count of a query to a variable

assign the row count of a query to a variable  
Kevin B.
 Re: assign the row count of a query to a variable  
PFC
 Re: assign the row count of a query to a variable  
Michael Fuhr
From:Kevin B.
Subject:assign the row count of a query to a variable
Date:Fri, 14 Jan 2005 15:43:04 -0500 (EST)
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';


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html
From:PFC
Subject:Re: assign the row count of a query to a variable
Date:Fri, 14 Jan 2005 22:13:57 +0100

var := count(*) from T;

or :

SELECT INTO var count(*) from T;

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly
From:Michael Fuhr
Subject:Re: assign the row count of a query to a variable
Date:Fri, 14 Jan 2005 14:15:48 -0700
On Fri, Jan 14, 2005 at 03:43:04PM -0500, Kevin B. wrote:

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

Please be more specific than "not having any luck." What are you
expecting to happen and what actually does happen?

> 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.

See the "Basic Statements" and "Expressions" sections of the PL/pgSQL
documentation. Either of the following should work:

var := count(*) FROM T;
SELECT INTO var count(*) FROM T;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
   

Copyright © 2006 knowledge-database   -   All rights reserved