grandnationalpostgresql
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| grandnationalpostgresql [2021/08/12 14:50] – z0hpvk | grandnationalpostgresql [2025/03/08 22:24] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 80: | Line 80: | ||
| LANGUAGE SQL | LANGUAGE SQL | ||
| as $$ | as $$ | ||
| - | delete from sweepstake; | + | delete from sweepstake; |
| - | insert into sweepstake (HorseNo, HorseName) (select horseno, name from horses); | + | |
| $$; | $$; | ||
| -- PostgreSQL Version 10 and Below | -- PostgreSQL Version 10 and Below | ||
| - | create or replace function | + | create or replace function |
| RETURNS void AS $$ | RETURNS void AS $$ | ||
| BEGIN | BEGIN | ||
| Line 94: | Line 94: | ||
| -- Performs one round of the draw. | -- Performs one round of the draw. | ||
| - | create or replace | + | create or replace |
| - | Cursor | + | RETURNS void AS $$ |
| - | select row_number() over (order by dbms_random.value | + | DECLARE |
| - | | + | |
| - | | + | |
| - | where row_num = PlayerID; | + | HorseNo, HorseName from sweepstake where playerid is null) as T1, |
| + | (select id PlayerID, name PlayerName from participants) | ||
| + | | ||
| BEGIN | BEGIN | ||
| - | FOR a IN C1 | + | |
| - | LOOP | + | LOOP |
| - | | + | Update sweepstake set PlayerID = a.PlayerID |
| - | using a.PlayerID, | + | END LOOP; |
| - | END LOOP; | + | |
| END; | END; | ||
| - | / | + | $$ LANGUAGE plpgsql; |
| -- Generate the sweepstake data | -- Generate the sweepstake data | ||
| - | create or replace | + | create or replace |
| - | Cursor | + | RETURNS void AS $$ |
| - | | + | DECLARE |
| - | | + | |
| - | NoParticipants | + | from participants, |
| - | NoHorses | + | where id = playerid order by horseno; |
| - | NoCycles | + | NoParticipants |
| - | begin | + | NoHorses |
| - | select count(*) into NoParticipants from participants; | + | NoCycles |
| - | select count(*) into NoHorses from horses; | + | BEGIN |
| - | NoCycles := ceil(NoHorses/ | + | select count(*) into NoParticipants from participants; |
| - | init; | + | select count(*) into NoHorses from horses; |
| + | NoCycles := ceil(NoHorses/ | ||
| + | | ||
| FOR a IN 1..NoCycles | FOR a IN 1..NoCycles | ||
| LOOP | LOOP | ||
| - | OneCycle; | + | |
| - | END LOOP; | + | |
| - | + | ||
| - | dbms_output.put_line(' '); | + | |
| - | dbms_output.put_line(' | + | |
| - | dbms_output.put_line(' | + | |
| - | FOR b in C1 | + | |
| - | LOOP | + | |
| - | | + | |
| END LOOP; | END LOOP; | ||
| - | end; | + | END; |
| - | / | + | $$ LANGUAGE plpgsql; |
| </ | </ | ||
| Line 156: | Line 151: | ||
| <code plsql> | <code plsql> | ||
| - | set serveroutput on | + | SELECT |
| - | BEGIN | + | |
| - | | + | |
| - | END; | + | |
| - | / | + | |
| </ | </ | ||
grandnationalpostgresql.1628779803.txt.gz · Last modified: (external edit)
