Home » SQL & PL/SQL » SQL & PL/SQL » Star printing in PL/SQL Program Problem (11g)
Star printing in PL/SQL Program Problem [message #579895] Sat, 16 March 2013 21:38 Go to next message
ashishpatel1992
Messages: 38
Registered: February 2013
Location: India
Member
I want the output to be like
1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1

Please correct my mistake in this program:-
declare 
v varchar2(50);
begin
for i in 1..10
loop
v:=v||' '||i;
display(v);
end loop;
v:=null;
for i in reverse 10..1
loop
v:=v||' '||i;
display(v);
end loop;
end;


Regards,
Ashish
Re: Star printing in PL/SQL Program Problem [message #579897 is a reply to message #579895] Sun, 17 March 2013 01:27 Go to previous messageGo to next message
Michel Cadot
Messages: 57613
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> declare 
  2  v varchar2(50);
  3  begin
  4  for i in 1..10
  5  loop
  6  v:=v||' '||i;
  7  display(v);
  8  end loop;
  9  v:=null;
 10  for i in reverse 10..1
 11  loop
 12  v:=v||' '||i;
 13  display(v);
 14  end loop;
 15  end;
 16  /
display(v);
*
ERROR at line 7:
ORA-06550: line 7, column 1:
PLS-00201: identifier 'DISPLAY' must be declared
ORA-06550: line 7, column 1:
PL/SQL: Statement ignored
ORA-06550: line 13, column 1:
PLS-00201: identifier 'DISPLAY' must be declared
ORA-06550: line 13, column 1:
PL/SQL: Statement ignored

What is DISPLAY?
Search in the documentation for DBMS_OUTPUT package.

Also please INDENT the code.
If you don't know, learn using SQL Formatter.

Regards
Michel
Re: Star printing in PL/SQL Program Problem [message #579899 is a reply to message #579897] Sun, 17 March 2013 01:48 Go to previous messageGo to next message
ashishpatel1992
Messages: 38
Registered: February 2013
Location: India
Member
Sorry. I forgot to mention display is a procedure.
You can replace display with dbms_output.put_line(<parameter>);
Re: Star printing in PL/SQL Program Problem [message #579902 is a reply to message #579899] Sun, 17 March 2013 02:12 Go to previous messageGo to next message
Michel Cadot
Messages: 57613
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Do it and post the new code FORMATTED.

Regards
Michel
Re: Star printing in PL/SQL Program Problem [message #579907 is a reply to message #579895] Sun, 17 March 2013 06:02 Go to previous messageGo to next message
Solomon Yakobson
Messages: 1797
Registered: January 2010
Senior Member
You misunderstand IN REVERSE. Regardless of IN or IN REVERSE lower_bound is first and upper bound is second. And in your second loop:

for i in reverse 10..1


lower_bound > upper bound, therefore no loop iterations are executed. If you change bounds around:

SQL> declare
  2  v varchar2(50);
  3  begin
  4  for i in 1..10
  5  loop
  6  v:=v||' '||i;
  7  dbms_output.put_line(v);
  8  end loop;
  9  v:=null;
 10  for i in reverse 1..10
 11  loop
 12  v:=v||' '||i;
 13  dbms_output.put_line(v);
 14  end loop;
 15  end;
 16  /
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
10
10 9
10 9 8
10 9 8 7
10 9 8 7 6
10 9 8 7 6 5
10 9 8 7 6 5 4
10 9 8 7 6 5 4 3
10 9 8 7 6 5 4 3 2
10 9 8 7 6 5 4 3 2 1

PL/SQL procedure successfully completed.

SQL>


What you want is:

declare
    v varchar2(50);
begin
    for i in 1..10 loop
      v := v || ' ' || i;
      dbms_output.put_line(v);
    end loop;
    --v:=null;
    for i in reverse 0..9 loop
      v := substr(v,1,i * 2);
      dbms_output.put_line(v);
    end loop;
end;
/
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

PL/SQL procedure successfully completed.

SQL>


SY.
Re: Star printing in PL/SQL Program Problem [message #579909 is a reply to message #579907] Sun, 17 March 2013 06:04 Go to previous message
Michel Cadot
Messages: 57613
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Hopefully you are there to post beginner's code, maybe you can open a site where everyone that wants to cheat at his exam can post the questions and get the answers.

Regards
Michel
Previous Topic: PL/SQL help needed in using Function inside query
Next Topic: SKIP INSERT ON WEEKDAYS - TRIGGER NOT WORKING
Goto Forum:
  


Current Time: Sun Apr 20 09:43:48 CDT 2014

Total time taken to generate the page: 0.04972 seconds