Re: Learning Plpgsql ?? - Mailing list pgsql-novice

From Jonathon Batson
Subject Re: Learning Plpgsql ??
Date
Msg-id 3E35BA54.6070003@octahedron.com.au
Whole thread Raw
In response to Re: Learning Plpgsql ??  (Jim Beckstrom <jrbeckstrom@sbcglobal.net>)
List pgsql-novice
Good Idea and it worked, had to play with quotes a bit, duh,
Note: the use of  quote_literal for seq's and quote_ident for table and
column objects.

CREATE or REPLACE FUNCTION up_seq() RETURNS text AS '
   DECLARE
      row RECORD;     qrystr TEXT;
   BEGIN
      -- select sequence information from seq table
    [ sequence_name, table_name, pk_column]
     FOR row IN SELECT * FROM swim_seq_temp LOOP

         qrystr :=
             ''SELECT setval(''
             || quote_literal(row.sequence_name)
             || '', ( SELECT max(''
             || quote_ident(row.pk_column)
             || '') FROM ''
             || quote_ident(row.table_name)
             || ''))'';
         EXECUTE qrystr;

     END LOOP;
   RETURN ''done'';
   END;
'  LANGUAGE 'plpgsql';





Jim Beckstrom wrote:

 > Coming from another newbie, here's what I use to set the sequence
 > following an import of text data.  I create seqence and table and
 > import one file at a time, for a one time conversion, so I don't need
 > the table of table names,etc., but that's a great idea, like a data
 > dictionary concept.  Would this work, modified to fit your loop syntax?
 >
 > select SETVAL('link_rep_link_rep_id_seq', (select max(link_rep_id)
 > from link_rep))
 >
 > Jim
 >
 >
 >
 > ---------------------------(end of broadcast)---------------------------
 > TIP 6: Have you searched our list archives?
 >
 > http://archives.postgresql.org
 >




pgsql-novice by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Passing parameters to a Trigger
Next
From: Jonathon Batson
Date:
Subject: Re: Learning Plpgsql ??