pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
assign the row count of a query to a variable
3+ messages / 3 participants
[nested] [flat]

* assign the row count of a query to a variable
@ 2005-01-14 20:43 Kevin B. <db@ke5in.com>
  2005-01-14 21:13 ` Re: assign the row count of a query to a variable PFC <lists@boutiquenumerique.com>
  2005-01-14 21:15 ` Re: assign the row count of a query to a variable Michael Fuhr <mike@fuhr.org>
  0 siblings, 2 replies; 3+ messages in thread

From: Kevin B. @ 2005-01-14 20:43 UTC (permalink / raw)
  To: 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';




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: assign the row count of a query to a variable
  2005-01-14 20:43 assign the row count of a query to a variable Kevin B. <db@ke5in.com>
@ 2005-01-14 21:13 ` PFC <lists@boutiquenumerique.com>
  1 sibling, 0 replies; 3+ messages in thread

From: PFC @ 2005-01-14 21:13 UTC (permalink / raw)
  To: db@ke5in.com; pgsql-sql


        var := count(*) from T;

or :

        SELECT INTO var count(*) from T;



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: assign the row count of a query to a variable
  2005-01-14 20:43 assign the row count of a query to a variable Kevin B. <db@ke5in.com>
@ 2005-01-14 21:15 ` Michael Fuhr <mike@fuhr.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Michael Fuhr @ 2005-01-14 21:15 UTC (permalink / raw)
  To: Kevin B. <db@ke5in.com>; +Cc: pgsql-sql

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/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-01-14 21:15 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-01-14 20:43 assign the row count of a query to a variable Kevin B. <db@ke5in.com>
2005-01-14 21:13 ` PFC <lists@boutiquenumerique.com>
2005-01-14 21:15 ` Michael Fuhr <mike@fuhr.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox