P°φkaz LOOP (SQL)

p°φkaz_LOOP ::= [ nßv∞Ütφ : ] LOOP p°φkaz à END LOOP [ nßv∞Ütφ ];

P°φkazy (resp. slo₧enΘ p°φkazy) obsa₧enΘ v p°φkazu LOOP se provßd∞jφ opakovan∞, dokud se neprovede p°φkaz LEAVE nebo dokud nedojde k v²jimce.

Je-li uvedeno koncovΘ nßv∞Ütφ, pak musφ b²t uvedeno takΘ stejnΘ poΦßteΦnφ nßv∞Ütφ. Uvnit° p°φkazu LOOP nesmφ b²t ₧ßdn² p°φkaz oznaΦen stejn²m nßv∞Ütφm.

Velmi ΦastΘ pou₧itφ nachßzφ LOOP p°i prochßzenφ zßznam∙ kurzoru otev°enΘho p°φkazem OPEN (prakticky jedinß mo₧nost, jak lze zßznamy projφt).

P°φklad pou₧itφ:

do tabulky vlo₧it 10 zßznam∙ a vzestupn∞ je oΦφslovat (zßpis ve vnit°nφm jazyce pomocφ SQL_execute)

var
  ss : string[500];
begin
  ss := "BEGIN ATOMIC "
        "DECLARE i INT; "     
        "SET i = 1; "
        "label: LOOP INSERT INTO Tab3 (cislo) VALUES (i); "
           "IF i >= 10 THEN LEAVE label; END IF; "
           "SET i = i + 1; "
        "END LOOP label; "
      "END";
  if SQL_execute(ss) then Signalize;
end.

pou₧itφ LOOP p°i prochßzenφ kurzoru:

PROCEDURE ZmenaCeniku();
BEGIN 
  DECLARE err_notfound BIT DEFAULT FALSE;
  DECLARE CONTINUE HANDLER FOR NOT FOUND 
    BEGIN 
      SET err_notfound=TRUE; 
    END;
  DECLARE curcen SENSITIVE CURSOR FOR 
    SELECT cislo_pol, cena
    FROM Cenik
  FOR UPDATE; 
  DECLARE Polozka CHAR(20); 
  DECLARE Cena NUMERIC(14,2); 
  OPEN curcen; 
  LabelLoop: LOOP 
     FETCH NEXT FROM curcen INTO Polozka, Cena; 
     IF err_notfound IS TRUE THEN LEAVE LabelLoop; END IF; 
     CASE SUBSTRING(Polozka FROM 1 FOR 1)
       WHEN "X" THEN 
         UPDATE SET Cena = Cena*0.9 WHERE CURRENT OF curcen;
       WHEN "A" THEN 
         UPDATE SET Cena = Cena*2 WHERE CURRENT OF curcen;
       ELSE
         DELETE WHERE CURRENT OF curcen;
     END CASE; 
  END LOOP LabelLoop; 
  CLOSE curcen; 
END;