Skip to content

Commit 1516a75

Browse files
committed
Oralce PL/SQL examples of recursion
1 parent 0d5d016 commit 1516a75

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

03_recursion/plsql/01_countdown.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SET SERVEROUTPUT ON
2+
DECLARE
3+
result NUMBER;
4+
5+
FUNCTION countdown(x NUMBER)
6+
RETURN NUMBER
7+
IS
8+
f NUMBER;
9+
10+
BEGIN
11+
dbms_output.put_line(x);
12+
IF x <= 0 THEN
13+
return x;
14+
ELSE
15+
f := countdown(x - 1);
16+
END IF;
17+
RETURN f;
18+
END;
19+
20+
BEGIN
21+
result := countdown(5);
22+
END;

03_recursion/plsql/02_greet.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE OR REPLACE PROCEDURE greet2(names IN VARCHAR2 )
2+
IS
3+
BEGIN
4+
dbms_output.put_line('how are you, ' || names || '?');
5+
END;
6+
/
7+
8+
CREATE OR REPLACE PROCEDURE bye
9+
IS
10+
BEGIN
11+
dbms_output.put_line('ok bye!');
12+
END;
13+
/
14+
15+
CREATE OR REPLACE PROCEDURE greet(names IN VARCHAR2)
16+
IS
17+
BEGIN
18+
dbms_output.put_line('hello, ' || names || '!');
19+
greet2(names);
20+
dbms_output.put_line('getting ready to say bye...');
21+
bye();
22+
END;
23+
/
24+
25+
SET SERVEROUTPUT ON
26+
BEGIN
27+
greet('adit');
28+
END;

03_recursion/plsql/03_factorial.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SET SERVEROUTPUT ON
2+
DECLARE
3+
result NUMBER;
4+
5+
FUNCTION factorial(x NUMBER)
6+
RETURN NUMBER
7+
IS
8+
f NUMBER;
9+
10+
BEGIN
11+
IF x = 0 THEN
12+
f := 1;
13+
ELSE
14+
f := x * factorial(x - 1);
15+
END IF;
16+
RETURN f;
17+
END;
18+
19+
BEGIN
20+
result := factorial(5);
21+
dbms_output.put_line(result);
22+
END;

0 commit comments

Comments
 (0)