Home > Net >  How to design rectangle pattern using PL/SQL
How to design rectangle pattern using PL/SQL

Time:07-08

I have been trying to write a code to print below pattern

Expected output :

***
***
***
***
***
***
***

But I know only one vertical pattern how to print it

DECLARE 
    v_num number :=7;
BEGIN
     FOR vcount in 1..v_num LOOP
            DBMS_OUTPUT.PUT_LINE('*');
     END LOOP;
END;

/

Need your input how to achieve expected output …

CodePudding user response:

Do you really need PL/SQL?

SQL> select '***'
  2  from dual
  3  connect by level <= 7;

'**
---
***
***
***
***
***
***
***

7 rows selected.

SQL>

If it must be PL/SQL:

SQL> begin
  2    for i in 1 .. 7 loop
  3      dbms_output.put_line('***');
  4    end loop;
  5  end;
  6  /
***
***
***
***
***
***
***

PL/SQL procedure successfully completed.

SQL>

If you want to make it generic, create a stored procedure:

SQL> create or replace procedure p_pattern
  2    (par_character in varchar2, par_number in number, par_rows in number)
  3  is
  4  begin
  5    for i in 1 .. par_rows loop
  6      dbms_output.put_line(rpad(par_character, par_number, par_character));
  7    end loop;
  8  end;
  9  /

Procedure created.

SQL> exec p_pattern('=', 5, 3);
=====
=====
=====

PL/SQL procedure successfully completed.

SQL> exec p_pattern('*', 3, 7);
***
***
***
***
***
***
***

PL/SQL procedure successfully completed.

SQL>
  • Related