pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jonah H. Harris <jonah.harris@gmail.com>
To: Ezequias R. da Rocha <ezequias@fastcon.com.br>
Cc: pgsql-sql@postgresql.org
Subject: Re: For loop
Date: Tue, 13 Mar 2007 09:39:27 -0400
Message-ID: <36e682920703130639i4bce82beg1bcab46dd16240ab@mail.gmail.com> (raw)
In-Reply-To: <45F6A793.1090904@fastcon.com.br>
References: <45F69D7C.4070304@fastcon.com.br>
	<45F6B1A2.A3DD.0030.0@indicator.be>
	<45F6B4EB.A3DD.0030.0@indicator.be>
	<45F6A793.1090904@fastcon.com.br>

On 3/13/07, Ezequias R. da Rocha <ezequias@fastcon.com.br> wrote:
> I quetion one more time. I must have a function ? Isn't another way to
> implement it without using functions ?

Not in PostgreSQL.  Here's a sample of something similar to what you were doing.

CREATE LANGUAGE plpgsql;

CREATE TABLE carga (
    id          NUMERIC,
    desc_txt    TEXT,
    PRIMARY KEY (id));

CREATE OR REPLACE FUNCTION for_loop_func (num_iter INTEGER)
RETURNS void AS
$$
DECLARE
    iter    NUMERIC;
    tmp_num NUMERIC;
BEGIN
    FOR iter IN 1 .. num_iter
    LOOP
        -- some computations
        tmp_num = iter * 2;

        INSERT INTO carga
                    (id, desc_txt)
             VALUES (tmp_num, 'My Text for ' || iter || '*2 = ' || tmp_num);
    END LOOP;
    RETURN;
END;
$$ LANGUAGE plpgsql;

--SELECT for_loop_func(1000000);
SELECT for_loop_func(10);



-- 
Jonah H. Harris, Software Architect | phone: 732.331.1324
EnterpriseDB Corporation            | fax: 732.331.1301
33 Wood Ave S, 3rd Floor            | jharris@enterprisedb.com
Iselin, New Jersey 08830            | http://www.enterprisedb.com/



view thread (16+ messages)  latest in thread

Message-ID: <36e682920703130639i4bce82beg1bcab46dd16240ab@mail.gmail.com>
Permalink:  ../36e682920703130639i4bce82beg1bcab46dd16240ab@mail.gmail.com/
Also on:    postgresql.org/message-id/36e682920703130639i4bce82beg1bcab46dd16240ab@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jonah.harris@gmail.com, ezequias@fastcon.com.br
  Subject: Re: For loop
  In-Reply-To: <36e682920703130639i4bce82beg1bcab46dd16240ab@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox