PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 10th, 2004, 09:01 AM
miha_ miha_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 miha_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question CRATE FUNCTION for dynamic generated tables

I have problems to install the following function

CREATE FUNCTION
countrow(text ) /* name of table */
RETURNS bigint
AS 'select count(*) from $1;'
LANGUAGE 'sql';


I have a pile of dynamic generated tables and don't want to use triggers. It's easer to change one function instead of hundred table triggers.

I know it's not common to use the function parameter for table name but maybe somebody has an idea and can help to make it work.

Thanx miha

Reply With Quote
  #2  
Old June 10th, 2004, 01:42 PM
metaBarf metaBarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 373 metaBarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 49 m 6 sec
Reputation Power: 6
here is a thread about using a param for a table name in a plpgsql function. Do you need it to be SQL? you can probably do the same there, I've never tried. you can call plpgsql functions the same way as sql functions, it depends on what you need to return.

Reply With Quote
  #3  
Old June 10th, 2004, 04:30 PM
miha_ miha_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 miha_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This hint...

Hey Thank you, this was my problem. now it looks a little mor complex, but it is working...

CREATE OR REPLACE FUNCTION countRow (text) RETURNS int8 AS '
DECLARE
table_name ALIAS FOR $1;
table_row INTEGER;
input_refc refcursor;
BEGIN
table_row := 0;
RAISE NOTICE ''table name = %'', table_name;
OPEN input_refc FOR EXECUTE ''SELECT count(*) FROM '' || quote_ident(table_name);
RAISE NOTICE ''tablefetch = %'', input_refc;
FETCH input_refc into table_row;
CLOSE input_refc;
RETURN table_row;
END; '
LANGUAGE plpgsql;


Thank you:-]

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > CRATE FUNCTION for dynamic generated tables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT