LOOPS IN SQL

 


SQL> set serveroutput on

SQL> declare

  2  i number(3) := 1;

  3  begin

  4  loop

  5  DBMS_OUTPUT.PUT_LINE(i);

  6  i := i+1;

  7  exit when i>10;

  8  end loop;

  9  end;

 10  /

1

2

3

4

5

6

7

8

9

10


PL/SQL procedure successfully completed.


SQL> declare

  2  i number(3) := 1;

  3  ch number(3);

  4  begin

  5  loop

  6  DBMS_OUTPUT.PUT_LINE(i);

  7  i := i+1;

  8  ch =&ch;

  9  exit when i>=ch;

 10  end loop;

 11  end;

 12  /

Enter value for ch: 10

old   8: ch =&ch;

new   8: ch =10;

ch =10;

   *

ERROR at line 8:

ORA-06550: line 8, column 4:

PLS-00103: Encountered the symbol "=" when expecting one of the following:

:= . ( @ % ;

The symbol ":= was inserted before "=" to continue.



SQL> declare

  2  i number(3) := 1;

  3  ch number(3);

  4  begin

  5  loop

  6  DBMS_OUTPUT.PUT_LINE(i);

  7  i := i+1;

  8  ch :=&ch;

  9  exit when i>=ch;

 10  end loop

 11  end;

 12  /

Enter value for ch: 4

old   8: ch :=&ch;

new   8: ch :=4;

end;

*

ERROR at line 11:

ORA-06550: line 11, column 1:

PLS-00103: Encountered the symbol "END" when expecting one of the following:

; <an identifier> <a double-quoted delimited-identifier>

The symbol ";" was substituted for "END" to continue.



SQL> declare

  2  i number(3) := 1;

  3  ch number(3);

  4  begin

  5  loop

  6  DBMS_OUTPUT.PUT_LINE(i);

  7  i := i+1;

  8  ch :=&ch;

  9  exit when i>=ch;

 10  end loop;

 11  end;

 12  /

Enter value for ch: 8

old   8: ch :=&ch;

new   8: ch :=8;

1

2

3

4

5

6

7


PL/SQL procedure successfully completed.


SQL>

SQL> ok saya cek dulu bro^Z

q

SP2-0734: unknown command beginning "ok saya ce..." - rest of line ignored.

SQL> declare

  2  fac number :=1;

  3  n number :=&1;

  4  begin

  5  while n>0 loop

  6  fac :=n*fac;

  7  n:=n-1;

  8  end loop;

  9  DBMS_OUTPUT.PUT_LINE(fac);

 10  end;

 11  /

Enter value for 1: 5

old   3: n number :=&1;

new   3: n number :=5;

120


PL/SQL procedure successfully completed.


SQL> declare

  2  fac number :=1;

  3  n number :=&1;

  4  begin

  5  while n>0 loop

  6  fac :=n*fac;

  7  n:=n-1;

  8  end loop;

  9  DBMS_OUTPUT.PUT_LINE('THE FACTORIAL VALUE IS'||fac);

 10  end;

 11  /

Enter value for 1: 7

old   3: n number :=&1;

new   3: n number :=7;

THE FACTORIAL VALUE IS5040


PL/SQL procedure successfully completed.


SQL> declare

  2  fac number :=1;

  3  n number :=&1;

  4  begin

  5  while n>0 loop

  6  fac :=n*fac;

  7  n:=n-1;

  8  end loop;

  9  DBMS_OUTPUT.PUT_LINE('THE FACTORIAL VALUE IS  '||fac);

 10  end;

 11  /

Enter value for 1: 7

old   3: n number :=&1;

new   3: n number :=7;

THE FACTORIAL VALUE IS  5040


PL/SQL procedure successfully completed.


SQL> declare

  2  i number(3);

  3  begin

  4  for i in 1..6

  5  loop

  6  DBMS_OUTPUT.PUT_LINE(i);

  7  end loop;

  8  end;

  9  /

1

2

3

4

5

6


PL/SQL procedure successfully completed.


SQL> declare

  2  i number(3);

  3  begin

  4  for i in reverse 1..6

  5  loop

  6  DBMS_OUTPUT.PUT_LINE(i);

  7

  8  end loop;

  9  end;

 10  /

6

5

4

3

2

1


PL/SQL procedure successfully completed.


SQL>

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.