Skip to Main Content
  • Questions
  • print the multiplication tables in PLSQL

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: July 21, 2022 - 9:24 am UTC

Last updated: July 29, 2022 - 3:34 pm UTC

Version: 12

Viewed 10K+ times! This question is

You Asked

Hi ,

I want print the multiplication table, if i enter 2 then need to display 2 multiplication , if i enter 50 then want print 50th multiplication.

Can you give both sql and plsql 2 examples ?

and Connor said...

SQL> set serverout on
SQL> declare
  2    factor int := 4;
  3  begin
  4    for i in 1 .. 12 loop
  5      dbms_output.put_line(factor*i);
  6    end loop;
  7  end;
  8  /
4
8
12
16
20
24
28
32
36
40
44
48

PL/SQL procedure successfully completed.


or

SQL> with factor as
  2  ( select 4 f from dual)
  3  select f*rownum
  4  from factor
  5  connect by level <= 12;

  F*ROWNUM
----------
         4
         8
        12
        16
        20
        24
        28
        32
        36
        40
        44
        48

12 rows selected.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

model boy !

Rajeshwaran, Jeyabal, July 27, 2022 - 2:55 pm UTC

Perhaps, the MODEL boy too won't disappoint us

SQL> variable n number
SQL> exec :n := 4;

PL/SQL procedure successfully completed.

SQL> ed
  1  select *
  2  from dual
  3  model
  4     return updated rows
  5     dimension by (0 x)
  6     measures( 0 val )
  7     rules(
  8             val[for x from 1 to 10
  9*                    increment 1] = cv(x) * :n );
SQL> /

    X    VAL
_____ ______
    1      4
    2      8
    3     12
    4     16
    5     20
    6     24
    7     28
    8     32
    9     36
   10     40

10 rows selected.

SQL>

Chris Saxon
July 29, 2022 - 3:34 pm UTC

Is using MODEL really necessary here?

I think 21c's new PL/SQL looping syntax is much more appropriate:

declare
  factor int := 4;
begin
  for i in factor .. ( factor * 12 ) 
  by factor
  loop
    dbms_output.put_line ( i );
  end loop;
end;
/

4
8
...
44
48

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.