User Tools

Site Tools


grandnationalpostgresql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
grandnationalpostgresql [2021/08/12 14:50] z0hpvkgrandnationalpostgresql [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);+ insert into sweepstake (HorseNo, HorseName) (select horseno, name from horses);
 $$; $$;
  
 -- PostgreSQL Version 10 and Below -- PostgreSQL Version 10 and Below
-create or replace function init10()+create or replace function init()
 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 procedure OneCycle as +create or replace function OneCycle() 
-Cursor C1 is select PlayerID, PlayerName, horseno, HorseName from ( +RETURNS void AS $$ 
-        select row_number() over (order by dbms_random.value ASC) row_num, +DECLARE 
-               HorseNo, HorseName from sweepstake where playerid is null), +  C1 CURSOR FOR select PlayerID, PlayerName, horseno, HorseName from ( 
-       (select id PlayerID, name PlayerName from participants) +           select row_number() over (order by random() ASC) row_num, 
-      where row_num = PlayerID;+                  HorseNo, HorseName from sweepstake where playerid is null) as T1
 +          (select id PlayerID, name PlayerName from participants) as T2 
 +         where row_num = PlayerID;
 BEGIN BEGIN
-FOR a IN C1 +  FOR a IN C1 
-LOOP +  LOOP 
-  EXECUTE IMMEDIATE 'Update sweepstake set PlayerID = :pid where HorseNo = :horseno'  +    Update sweepstake set PlayerID = a.PlayerID where HorseNo = a.HorseNo; 
-                     using a.PlayerID, a.HorseNo; +  END LOOP;
-END LOOP;+
 END; END;
-/+$$ LANGUAGE plpgsql;
  
 -- Generate the sweepstake data -- Generate the sweepstake data
-create or replace procedure GenSweepstake as +create or replace function GenSweepstake() 
-Cursor C1 is select name, horseno, horsename +RETURNS void AS $$ 
-             from participants, sweepstake +DECLARE 
-             where id = playerid order by horseno; +  C1 CURSOR FOR select name, horseno, horsename 
-NoParticipants number(2)+                from participants, sweepstake 
-NoHorses       number(3)+                where id = playerid order by horseno; 
-NoCycles       number(2)+  NoParticipants real
-begin +  NoHorses       real
-select count(*) into NoParticipants from participants; +  NoCycles       smallint
-select count(*) into NoHorses from horses; +BEGIN 
-NoCycles := ceil(NoHorses/NoParticipants); +  select count(*) into NoParticipants from participants; 
-  init;+  select count(*) into NoHorses from horses; 
 +  NoCycles := ceil(NoHorses/NoParticipants); 
 +    perform init();
 FOR a IN 1..NoCycles FOR a IN 1..NoCycles
 LOOP LOOP
-  OneCycle+  perform OneCycle();
-END LOOP; +
- +
-dbms_output.put_line(' '); +
-dbms_output.put_line('PLAYER    NUMBER  HORSE NAME'); +
-dbms_output.put_line('------    ------  ------------------'); +
-FOR b in C1 +
-LOOP +
-   dbms_output.put_line(rpad(b.name, 10) || rpad(b.horseno, 8) || rpad(b.horsename, 30));+
 END LOOP; END LOOP;
-end+END
-/+$$ LANGUAGE plpgsql;
 </code> </code>
  
Line 156: Line 151:
  
 <code plsql> <code plsql>
-set serveroutput on +SELECT GenSweepstake;
-BEGIN +
-  GenSweepstake(); +
-END; +
-/+
 </code> </code>
grandnationalpostgresql.1628779803.txt.gz · Last modified: 2025/03/08 22:23 (external edit)