Re: Saving score of 3 players into a table

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Saving score of 3 players into a table
Date: 2011-10-26 09:56:36
Message-ID: CAF-3MvMpTyN_utr4T3PB4JmiUocG4uRRsSXk_6dXrSCBpS+rjw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 26 October 2011 10:08, Alexander Farber <alexander(dot)farber(at)gmail(dot)com> wrote:

>    create table pref_games {
>            gid serial,
>            rounds integer not null,
>            finished timestamp default current_timestamp
>    }

> then how do I find the new game id after I've just created it here:
>
>    create or replace function pref_insert_scores(
>        _uid0 varchar, _money0 integer, _quit0 boolean,
>        _uid1 varchar, _money1 integer, _quit1 boolean,
>        _uid2 varchar, _money2 integer, _quit2 boolean,
>        _rounds integer) returns void as $BODY$
>            begin
>
>            insert into pref_games (rounds) values (_rounds);
>
>            -- XXX how do I get the _gid of this new game?

Use insert .. returning gid with a cursor:

declare
game_cur cursor (n integer) for insert into pref_games (rounds)
values (n) returning gid;
_rec record;
begin
for _rec in game_cur(_rounds) loop
insert into pref_scores (uid, _rec.gid, money, quit) values...
end loop;

>            insert into pref_scores (uid, gid, money, quit)
>                values(_uid0, _gid, _money0, _quit0);
>
>            insert into pref_scores (uid, gid, money, quit)
>                values(_uid1, _gid, _money1, _quit1);
>
>            insert into pref_scores (uid, gid, money, quit)
>                values(_uid2, _gid, _money2, _quit2);
>            end;
>    $BODY$ language plpgsql;
>
> Thank you! I've listed few more details at
> http://stackoverflow.com/questions/7899995/save-scores-of-3-players-per-game-into-postgresql
>
> Regards
> Alex
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Guettler 2011-10-26 10:35:12 Docs: Add Version Info: New since Version x.y ....
Previous Message mailtolouis2020-postgres@yahoo.com 2011-10-26 09:44:12 pglesslog for Postgres 9.1.1