dbTalk Databases Forums  

Re: counting records of schema

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Re: counting records of schema in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ian Barwick
 
Posts: n/a

Default Re: counting records of schema - 10-30-2004 , 05:24 PM






On Tue, 26 Oct 2004 08:03:26 +0200, tom.zschockelt (AT) flender (DOT) com
<tom.zschockelt (AT) flender (DOT) com> wrote:
Quote:
I need to know how many records are in a specific schema of a database.

I've tried with a function but there are still problems
Providing details of the problems usually helps ;-).

Quote:
Can you give me some hints :

-- Function: count_records(myschema varchar)

-- DROP FUNCTION count_records("varchar");

CREATE OR REPLACE FUNCTION count_records("varchar")
RETURNS int8 AS
$BODY$DECLARE
anzahl bigint := 0;
summe bigint := 0;
ds RECORD;
tabellenname varchar(100);
BEGIN
FOR ds IN select * from pg_tables where schemaname = myschema LOOP
'myschema' is not defined anywhere - I presume it's the alias for the
function's VARCHAR argument?

Quote:
tabellenname := quote_ident(ds.schemaname) || '.' ||
quote_ident(ds.tablename);
EXECUTE 'SELECT count(*) FROM ' || tabellenname INTO quote_ident(anzahl);
You can't (directly) extract the result of a dynamically-created
SELECT using EXECUTE, see
http://www.postgresql.org/docs/7.4/s...-EXECUTING-DYN
for further information and hints.

Quote:
summe := summe + anzahl;
END LOOP;
return summe;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE;
BTW you don't need to specify VOLATILE here - it's the default - and
STABLE might be the appropriate choice anyway.

HTH

Ian Barwick
barwick (AT) gmail (DOT) com

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

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.