Lädt...


🔧 LOOP, EXIT, CONTINUE and WHILE statements in PostgreSQL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

*Memos:

<LOOP statement>

A LOOP statement can keep running zero or more SQL queries in it.

Now, you can create the DO statement with a LOOP statement as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    RAISE INFO '%', num;
    num := num + 1;
  END LOOP;
END
$$;

*Memos:

Then, it causes an infinite loop, then the infinite loop is automatically stopped(killed) with the error as shown below. *My answer explains how to manually stop(kill) an infinite loop on psql:

INFO:  0
INFO:  1
INFO:  2
...
INFO:  86760
INFO:  86761
INFO:  86762
ERROR:  canceling statement due to user request
CONTEXT:  PL/pgSQL function inline_code_block line 7 at RAISE

<EXIT statement>

An EXIT statement can exit the loop anytime or when the condition is true.

Now, you can use an EXIT statement in a LOOP statement as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    EXIT; -- Here
    RAISE INFO '%', num;
    num := num + 1;
  END LOOP;
END
$$;

Then, it doesn't raise any messages in the loop because the EXIT statement exits the loop just before the RAISE statement as shown below:

DO

Next, you can use a WHEN clause with the condition num = 3 as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    EXIT WHEN num = 3; -- Here
    RAISE INFO '%', num;
    num := num + 1;
  END LOOP;
END
$$;

*You can replace EXIT WHEN ... with IF num ... as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    -- EXIT WHEN num = 3;
    IF num = 3 THEN -- Here
      EXIT;         -- Here
    END IF;         -- Here
    RAISE INFO '%', num;
    num := num + 1;
  END LOOP;
END
$$;

Then, it raises the messages in the loop, then exits the loop when num = 3 as shown below:

INFO:  0
INFO:  1
INFO:  2
DO

Next, you can exit inner and outer loops when num >= 2 and num = 4 respectively as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    LOOP
      EXIT WHEN num >= 2; -- Here
      RAISE INFO 'Inner loop:%', num;
      num := num + 1;
    END LOOP;
    EXIT WHEN num = 4; -- Here
    RAISE INFO 'Outer loop:%', num;
    num := num + 1;
  END LOOP;
END
$$;

Then, it raises the messages in the inner and outer loops, then exits the inner and outer loops when num >= 2 and num = 4 respectively as shown below:

INFO:  Inner loop:0
INFO:  Inner loop:1
INFO:  Outer loop:2
INFO:  Outer loop:3
DO

<CONTINUE statement>

A CONTINUE statement can exit the current iteration of the loop anytime or when the condition is true, then starts the next iteration of the loop.

Now, you can use a CONTINUE statement in a LOOP statement as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    num := num + 1;
    EXIT WHEN num = 4;
    CONTINUE; -- Here
    RAISE INFO '%', num;
  END LOOP;
END
$$;

Then, it doesn't raise any messages in the loop because the CONTINUE statement exits the current iteration of the loop just before the RAISE statement, then starts the next iteration of the loop as shown below:

DO

Next, you can use a WHEN clause with the condition num = 2 as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    num := num + 1;
    EXIT WHEN num = 4;
    CONTINUE WHEN num = 2; -- Here
    RAISE INFO '%', num;
  END LOOP;
END
$$;

*You can replace CONTINUE WHEN ... with IF num ... as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    num := num + 1;
    EXIT WHEN num = 4;
    -- CONTINUE WHEN num = 2;
    IF num = 2 THEN -- Here
      CONTINUE;     -- Here
    END IF;         -- Here
    RAISE INFO '%', num;
  END LOOP;
END
$$;

Then, it raises the messages in the loop except when num = 2 as shown below:

INFO:  1
INFO:  3
DO

Next, you can exit the current iteration of inner and outer loops when num = 2 and num = 4 respectively as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  LOOP
    LOOP
      num := num + 1;
      EXIT WHEN num >= 3;
      CONTINUE WHEN num = 2; -- Here
      RAISE INFO 'Inner loop:%', num;
    END LOOP;
    EXIT WHEN num >= 6;
    CONTINUE WHEN num = 4; -- Here
    RAISE INFO 'Outer loop:%', num;
  END LOOP;
END
$$;

Then, it raises the messages in the inner and outer loops except when num = 2 and num = 4 respectively as shown below:

INFO:  Inner loop:1
INFO:  Outer loop:3
INFO:  Outer loop:5
DO

<WHILE statement>

A WHILE statement can repeat a LOOP statement as long as the condition is true.

Now, you can use the WHILE statement whose condition is num < 3 with a LOOP statement as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  WHILE num < 3 LOOP
    RAISE INFO '%', num;
    num := num + 1;
  END LOOP;
END
$$;

Then, it raises the messages in the loop, then exits the loop while num < 3 as shown below:

INFO:  0
INFO:  1
INFO:  2
DO

Next, you can use the inner and outer WHILE statements whose conditions are num < 3 and num < 5 respectively as shown below:

DO $$
DECLARE
  num INT := 0;
BEGIN
  WHILE num < 5 LOOP
    WHILE num < 3 LOOP
      RAISE INFO 'Inner loop:%', num;
      num := num + 1;
    END LOOP;
    RAISE INFO 'Outer loop:%', num;
    num := num + 1;
  END LOOP;
END
$$;

Then, it raises the messages in the inner and outer loops while num < 3 and num < 5 respectively as shown below:

INFO:  Inner loop:0
INFO:  Inner loop:1
INFO:  Inner loop:2
INFO:  Outer loop:3
INFO:  Outer loop:4
DO
...

🔧 LOOP, EXIT, CONTINUE and WHILE statements in PostgreSQL


📈 70.52 Punkte
🔧 Programmierung

🔧 Loop Control statements in Python : break, continue, pass


📈 37.54 Punkte
🔧 Programmierung

🔧 Control Flow Statements ( for, while, do-while ) in dart (Bangla)


📈 33.29 Punkte
🔧 Programmierung

📰 Linux bash exit status and how to set exit status in bash


📈 29.27 Punkte
📰 IT Security Nachrichten

🐧 Bash exit 1 and exit 0 – What’s the Difference?


📈 29.27 Punkte
🐧 Linux Tipps

🐧 Break and Continue Statements in C#


📈 29.12 Punkte
🐧 Linux Tipps

🔧 break and continue statements in C programming.


📈 29.12 Punkte
🔧 Programmierung

⚠️ [shellcode] Linux/x86 - exit(0) / exit(1) Shellcode (3/4 bytes)


📈 28.06 Punkte
⚠️ PoC

⚠️ #0daytoday #Linux/x86 - exit(0) / exit(1) Shellcode (3/4 bytes) [shellcode #0day #Exploit]


📈 28.06 Punkte
⚠️ PoC

🎥 re:publica 2024: Vom Blinkist-Exit zum Multimillionärs-Exit


📈 28.06 Punkte
🎥 Video | Youtube

🕵️ Google TensorFlow up to 2.1.3/2.2.2/2.3.2/2.4.1 lite/kernels/while.cc While infinite loop


📈 27.31 Punkte
🕵️ Sicherheitslücken

🔧 COBOL Tutorial Series: Loop statements - Session 4


📈 25.27 Punkte
🔧 Programmierung

🔧 Speed Up Repetitive PostgreSQL Statements With \gexec


📈 24.52 Punkte
🔧 Programmierung

🍏 Apple Logo Keeps Blinking? Here’s How to Exit a Reboot Loop


📈 23.67 Punkte
🍏 iOS / Mac OS

🔧 🚀 Mastering Loop Control in C Programming: Leveraging break and continue 🌟


📈 23.13 Punkte
🔧 Programmierung

📰 Android Sales Continue to Decline, While iOS Grows in Most Regions


📈 21.11 Punkte
📰 IT Security Nachrichten

📰 Android Sales Continue to Decline, While iOS Grows in Most Regions


📈 21.11 Punkte
📰 IT Security Nachrichten

🪟 Continue on PC is going away, while Edge is here to stay


📈 21.11 Punkte
🪟 Windows Tipps

🪟 Samsung expects disappointing dip in operating profit for Q3 2024 while sales continue to surge


📈 21.11 Punkte
🪟 Windows Tipps

🔧 Day 16 - Slicing,for loop and nested loop


📈 20.5 Punkte
🔧 Programmierung

🍏 Grab up to 70% off Solo Loop and Braided Solo Loop Apple Watch bands


📈 20.5 Punkte
🍏 iOS / Mac OS

🔧 Exploring While and DoWhile in Java: Master Loop Structures with Practical Examples


📈 19.69 Punkte
🔧 Programmierung

🔧 Exploring While and DoWhile in Java: Master Loop Structures with Practical Examples


📈 19.69 Punkte
🔧 Programmierung

🔧 Solidity Crash Course - Part 05: While,For loop, Event, Arrays and Struct


📈 19.69 Punkte
🔧 Programmierung

🔧 switch and while loop:


📈 19.69 Punkte
🔧 Programmierung

🔧 switch and while loop:


📈 19.69 Punkte
🔧 Programmierung

🔧 while loop programes - sum and count


📈 19.69 Punkte
🔧 Programmierung

🔧 while and do loop:


📈 19.69 Punkte
🔧 Programmierung

📰 The Loop Kodi Addon: How to Install Loop for Kodi


📈 19.29 Punkte
🖥️ Betriebssysteme

🕵️ QEMU 5.0.0 TD List hw/usb/hcd-ohci.c Infinite Loop infinite loop


📈 19.29 Punkte
🕵️ Sicherheitslücken

📰 Microsoft Loop: Was genau ist die Loop Komponente?


📈 19.29 Punkte
🖥️ Betriebssysteme

matomo