i need to print numbers in pl sql in the given format?

7.9k Views Asked by At

I need to print numbers in pl/sql in the given format?

1
1 2 
1 2 3
1 2 3 4
1 2 3 4 5

also can you tell me how to print the next two commands in the same line in the output screen.

dbms_output.put_line(j);
dbms_output.put_line(j+1);
2

There are 2 best solutions below

0
On BEST ANSWER

To print multiple outputs on the same line, use dbms_output.put instead of dbms_output.put_line.

CAVEAT When using dbms_output.put, you have to flush the buffer afterwards - otherwise, your output won't appear on the screen.

Your original question can be solved by using two for loops:

begin
  for i in 1 .. 10
  loop
    for j in 1 .. i
    loop
      dbms_output.put(to_char(j) || ' ');
    end loop;
    dbms_output.new_line;
  end loop;
end;
0
On

You could do this in plain SQL instead of PL/SQL as follows:

select to_char(sum(res) over(order by res)) as res
  from ( select sum(power(10, level - 1)) over(order by level) as res
           from dual
        connect by level <= 5 )

Result:

RES
--------------------
1
12
123
1234
12345

To_char() function is used just to align result to the left.