Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 01:11 AM
Robert Fitzpatrick
Guest
 
Posts: n/a
Default Remembering values in pl/pgsql

Running 7.4.2, I have a pl/pgsql function with a WHILE LOOP that inserts
records a set number of times. Each time, the function generates a
random number and INSERT INTO a table, if that number has been used
already, I need to enter all fields leaving the incremented number NULL.
How can I keep track of the numbers already used, does it support arrays
and them? I tried a SELECT INTO (shown below), but I guess the INSERT
does not commit until the LOOP is finished. Is there a way to commit the
record before the next item in the LOOP?

WHILE sampleno <= units_to_test LOOP
randno := random();
randresult := bldginfo.units_count*randno;
randround := CEIL(randresult);
SELECT INTO checkit COUNT(public.tblhud74b.similar_group_id) AS
unit_count FROM public.tblhud74b WHERE
(public.tblhud74b.similar_group_id = bldginfo.similar_group_id) AND
(public.tblhud74b.rounded = randround);
IF NOT FOUND THEN
INSERT INTO tblhud74b VALUES (bldginfo.similar_group_id,
bldginfo.units_count, randno, randresult, randround, sampleno);
sampleno := sampleno + 1;
nounits := nounits + 1;
ELSE
INSERT INTO tblhud74b VALUES (bldginfo.similar_group_id,
bldginfo.units_count, randno, randresult, randround);
END IF;
END LOOP;

--
Robert


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles