grandnationalpostgresql
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| grandnationalpostgresql [2021/08/12 14:30] – created z0hpvk | grandnationalpostgresql [2025/03/08 22:24] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 76: | Line 76: | ||
| <code plsql> | <code plsql> | ||
| -- Initialise the SWEEPSTAKE table | -- Initialise the SWEEPSTAKE table | ||
| - | create or replace procedure init as | + | -- PostgreSQL Version 11 and Above |
| - | begin | + | create or replace procedure init() |
| - | | + | LANGUAGE SQL |
| - | | + | as $$ |
| - | end; | + | |
| - | / | + | |
| + | $$; | ||
| + | |||
| + | -- PostgreSQL Version 10 and Below | ||
| + | create or replace function init() | ||
| + | RETURNS void AS $$ | ||
| + | BEGIN | ||
| + | | ||
| + | | ||
| + | END; | ||
| + | $$ LANGUAGE plpgsql; | ||
| -- 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; | END LOOP; | ||
| - | + | END; | |
| - | dbms_output.put_line(' | + | $$ LANGUAGE plpgsql; |
| - | dbms_output.put_line(' | + | |
| - | dbms_output.put_line(' | + | |
| - | FOR b in C1 | + | |
| - | LOOP | + | |
| - | | + | |
| - | END LOOP; | + | |
| - | end; | + | |
| - | / | + | |
| </ | </ | ||
| Line 146: | Line 151: | ||
| <code plsql> | <code plsql> | ||
| - | set serveroutput on | + | SELECT |
| - | BEGIN | + | |
| - | | + | |
| - | END; | + | |
| - | / | + | |
| </ | </ | ||
grandnationalpostgresql.1628778641.txt.gz · Last modified: (external edit)
