Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 3CCDF9FA8EF for ; Fri, 27 Oct 2006 13:47:50 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 48650-01 for ; Fri, 27 Oct 2006 16:47:44 +0000 (UTC) X-Greylist: delayed 00:17:48.843687 by SQLgrey- Received: from e-cocreate.com (ottawa-hs-206-191-28-238.s-ip.magma.ca [206.191.28.238]) by postgresql.org (Postfix) with ESMTP id A100F9F9F96 for ; Fri, 27 Oct 2006 13:47:43 -0300 (ADT) Received: from laptop [192.168.1.101] by e-cocreate.com with ESMTP (SMTPD32-6.06) id A2136800DE; Fri, 27 Oct 2006 12:21:39 -0400 From: "Doug Hyde" To: Subject: Function to reset sequence..... Date: Fri, 27 Oct 2006 12:28:53 -0400 Message-ID: <001601c6f9e4$fe2d5000$6501a8c0@laptop> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 In-Reply-To: <200610231049.16507.fluca1978@infinito.it> Thread-Index: Acb2fxde6nsVaTbSTES6hYnhxk/HoADZA6ug X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.135 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200610/223 X-Sequence-Number: 26612 I am a real newbie with PL/pgSQL, and with postgres, but here goes. The problem: when you import data into postgresql, there is no way to set the proper sequence on the primary key. For example, I could have a table with 2000 rows, some of which have been added/deleted in time, so the nextval for the sequence should be 3301. When you set the table up with sql as: CREATE TABLE "public"."tblcompany"( "intcompany" SERIAL PRIMARY KEY, "txtcompanyname_1" varchar(255) , ... ); The nextval is set by default to 1. To overcome this, and avoid manually resetting the key (which is error prone), my strategy is (1) create the structure, (2) import the data, and then (3) reset the primary key. I have written sql to accomplish the first two which I have working well; the third is more complicated. What I would like to try is pasted below, after messing around - I haven't really got it even close to working: CREATE OR REPLACE FUNCTION fixsequence() returns void AS $$ BEGIN x RECORD; temp int; -- set up a loop for the tables in the database FOR x IN SELECT table_name, column_name FROM information_schema.key_column_usage NATURAL JOIN information_schema.table_constraints WHERE constraint_type = 'PRIMARY KEY' AND ordinal_position = 1 ORDER BY 1; -- begin loop LOOP -- get the max value of the primary key and add 1 select max(x.column_name)+1 as temp from x.tablename; -- get the seqence name for the table, sequence name always includes the table name of the pimary key select relname as seq_name from pg_class where relkind = 'S' and relname like x.table_name'%'; -- now reset the sequence for that table SELECT setval(seq_name, temp); END LOOP; END; $LANGUAGE 'plpgsql'; Before I mess up my data, will this (or something like it work) as I have little confidence? I am having trouble with combining variables with wildcards (middle of the loop). Thanks for any support. Doug