I am working on a project, where I come up with a problem, I need a PLSQL program which will execute the following patterns, like increasing numbers, stars, hashes etc.
1
12
123
1234
12345
CodePudding user response:
You can use such a hierarchical query
WITH t AS
(
SELECT LISTAGG(level) WITHIN GROUP (ORDER BY level) AS nr, MAX(level) AS lvl
FROM dual
CONNECT BY level <= 5
)
SELECT SUBSTR(nr,1,level) AS nr
FROM t
CONNECT BY level <= lvl
If needed to be written within a PL/SQL code block, then use a FOR LOOP along with the query above
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
FOR c IN
(
<above query>
)
LOOP
DBMS_OUTPUT.PUT_LINE(c.nr);
END LOOP;
END;
/
CodePudding user response:
Simply use two loops:
BEGIN
FOR i IN 1 .. 5 LOOP
FOR j IN 1 .. i LOOP
DBMS_OUTPUT.PUT(j);
END LOOP;
DBMS_OUTPUT.NEW_LINE();
END LOOP;
END;
/
Which outputs:
1 12 123 1234 12345
db<>fiddle here