Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to design rectangle pattern using PL/SQL

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 …

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading