469,300 Members | 2,334 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Oracle PL/SQL 9i/10g - Sample Programs

debasisdas
8,127 Expert 4TB
Sample PL/SQL programs
=====================

To print EVEN numbers 1--100

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2. FOR I IN  1..100 LOOP
  3. --if the number is divisible by 2 then it is a even number.
  4. IF MOD(I,2)=0 THEN 
  5. --display the number.
  6. dbms_output.put_line(I);
  7. END IF;
  8. END LOOP;
  9. END;
  10.  
Sample program to REVERSE A NUMBER

Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. MNUM INTEGER;
  3. MREV  INTEGER:=0;
  4. BEGIN
  5. MNUM:=&MNUM;
  6. WHILE(MNUM>0) LOOP
  7. MREV:=MREV*10 +MOD(MNUM,10);
  8. MNUM:=TRUNC(MNUM/10);
  9. END LOOP;
  10. DBMS_OUTPUT.PUT_LINE('THE REVERSE IS: '||MREV);
  11. END;
  12.  
Sample program showing use of XOR (Exclusive OR )

Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. I BOOLEAN:=FALSE;
  3. J BOOLEAN:=FALSE;
  4. K BOOLEAN;
  5. BEGIN
  6. K:=XOR(I,J);
  7. IF K=TRUE THEN
  8. DBMS_OUTPUT.PUT_LINE('TRUE');
  9. ELSE
  10. DBMS_OUTPUT.PUT_LINE('FALSE');
  11. END IF;
  12. END;
  13.  
Sample program showing use of EXECUTE IMMEDIATE

Expand|Select|Wrap|Line Numbers
  1. declare
  2. tablename varchar2(100):='&tablename';
  3. rows_deleted number;
  4. str varchar2(30);
  5. begin
  6. str := 'delete  from ';
  7. str :=str||tablename;
  8. execute immediate str;
  9. rows_deleted:=sql%rowcount;
  10. dbms_output.put_line(to_char(rows_deleted));
  11. end;
  12.  
Sample program to REVERSE A STRING

Expand|Select|Wrap|Line Numbers
  1. declare
  2. i varchar2(20):='&i';
  3. j varchar2(20);
  4. k int;
  5. l int;
  6. begin
  7. k:=length(i);
  8. for l in reverse 1..k loop
  9. j:=j||substr(i,l,1);
  10. end loop;
  11. dbms_output.put_line(j);
  12. end;
  13.  
SAMPLE PROGRAM TO PRINT ARMSTRONG NUMBER

Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. A NUMBER;
  3. B NUMBER;
  4. BEGIN
  5. FOR I IN 1..500 LOOP
  6. A:=I; 
  7. B:=0;
  8. LOOP
  9. EXIT WHEN A<=0;
  10. B:=B+POWER(MOD(A,10),3);
  11. A:=TRUNC(A/10);
  12. END LOOP;
  13. IF B=I THEN
  14. DBMS_OUTPUT.PUT_LINE(I||' IS ARMSTRONG NUMBER');
  15. END IF;
  16. END LOOP;
  17. END;
  18.  
SOME SAMPLE CODE

Expand|Select|Wrap|Line Numbers
  1. declare type nn is table of number index by pls_integer;
  2.  mm nn;
  3.  n number:=1;
  4.  s number:=1;
  5.  begin
  6.  for rec in (select empno from emp) loop
  7.  mm(n):=rec.empno;
  8.  n:=n+1;
  9.  end loop;
  10.  while s<n loop
  11.  dbms_output.put_line(mm(s));
  12.  s:=s+1;
  13.  end loop;
  14.  end ;
  15.  
May 10 '07 #1
0 30279

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

28 posts views Thread by prunoki | last post: by
13 posts views Thread by BigDaDDY | last post: by
11 posts views Thread by Rosco | last post: by
10 posts views Thread by www.hitechskill.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.