X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F2396D1B3A8 for ; Wed, 16 Jun 2004 19:11:44 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 52041-05 for ; Wed, 16 Jun 2004 22:11:41 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id E2D82D1B20E for ; Wed, 16 Jun 2004 19:11:35 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i5GMBYQC091681 for ; Wed, 16 Jun 2004 22:11:34 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i5GLnpMC087670 for pgsql-sql@postgresql.org; Wed, 16 Jun 2004 21:49:51 GMT From: ctrl@altonsys.com (ctrl) X-Newsgroups: comp.databases.postgresql.sql Subject: help with Postgres function Date: 16 Jun 2004 14:49:43 -0700 Organization: http://groups.google.com Lines: 46 Message-ID: <46a31c4d.0406161349.38d3a34b@posting.google.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/174 X-Sequence-Number: 18119 Hello. I'd appreciate any help with a function I'm writing. I have a simple table like this: CREATE TABLE websites ( id BIGSERIAL not null primary key, domain character varying(256) NOT NULL, last_fetch timestamp without time zone DEFAULT 'now()', crawl_status smallint not null DEFAULT 1 ); and my function retrieves the next available row in this table, by age (the oldest inserted and with status 1) I have written this but I have problems with it...and also it doesn't seems right to me...from coding style and performance point of view. CREATE OR REPLACE FUNCTION getNextWebsiteForCrawl(integer) RETURNS website AS ' DECLARE my_record RECORD; w website%rowtype; count smallint; BEGIN SELECT id, domain into my_record FROM websites WHERE crawl_status=1 AND date(last_fetch) > (current_timestamp - interval ''$1 days'') ORDER BY last_fetch LIMIT 1; select count(*) into count from my_record; if count > 0 then w.id := my_record.id; w.domain := my_record.domain; update websites set crawl_status=2 where id = my_record.id; end IF; return w; END; ' LANGUAGE plpgsql; The reason I have that IF is for when there are no more rows available... when I call this function (SELECT * FROM getNextWebsiteForCrawl(5)) I get an error: WARNING: Error occurred while executing PL/pgSQL function getnextwebsiteforcrawl WARNING: line 8 at select into variables ERROR: parser: parse error at or near "(" at character 13 Could somebody good show me how to do it better? Thanks a lot!