Search Postgresql Archives

Re: Saving score of 3 players into a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 26 October 2011 10:08, Alexander Farber <alexander.farber@xxxxxxxxx> 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@xxxxxxxxxxxxxx)
> 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.

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux