Vous êtes sur la page 1sur 6

Sum of number between 1 to 100 DECLARE a number; sum1 number :=0; BEGIN a:=1; loop sum1:=sum1+a; exit when

(a=100); a:=a+1; END loop; DBMS_OUTPUT.PUT_LINE('Sum between 1 to 100 is '||sum1); END; Calculate The Net Salary DECLARE ename varchar2(15); basic number; da number; hra number; pf number; netsalary number; BEGIN ename:='&ename'; basic:=&basic; da:=basic * (30/100); hra:=basic * (10/100); if (basic < 8000) then pf:=basic * (8/100); elsif (basic >= 8000 and basic <= 16000) then pf:=basic * (10/100); END if; netsalary:=basic + da + hra -pf; DBMS_OUTPUT.PUT_LINE('Employee name : ' || ename); DBMS_OUTPUT.PUT_LINE('ProvidEND Fund : ' || pf); DBMS_OUTPUT.PUT_LINE('Net salary : ' || netsalary); END; Calculate area and perimeter of circle DECLARE pi constant number(3,2) := 3.14; radius number(7); area number(13,2); perimeter number(13,2); BEGIN radius := &n; perimeter:=2*pi*radius; area := pi* power(radius,2); DBMS_OUTPUT.PUT_LINE('Area'||area||' '||'perimeter'||perimeter); END; Sum of odd number between 1 to 100 DECLARE

NUM number:=1; Sum number:=0; BEGIN loop NUM1 := NUM+2; Sum:=Sum+Num1; exit when NUM1=100; END loop; DBMS_OUTPUT.PUT_LINE (sum); END; Using Iteration In Pl/sql DECLARE NUM number:=&N; BEGIN if(NUM>0) DBMS_OUTPUT.PUT_LINE (NUM||'Natural Number'); else DBMS_OUTPUT.PUT_LINE (NUM||'Wrong Number'); exit when NUM1=100; END if; END; Looping(Print Even Number 1 to 100) DECLARE NUM1 number:=0; BEGIN loop NUM1 := NUM1+2; DBMS_OUTPUT.PUT_LINE (NUM1||','); exit when NUM1=100; END loop; END; Reverse Of a Number DECLARE num number:=&n; rev number:=0; BEGIN while(num>0) Loop rev=rev*10+mod(num,10); num=num/10; END loop; DBMS_OUTPUT.PUT_LINE("Reverse of number is '|| rev); END; Factorial of a number DECLARE num number:= &num; fact number:= 1; temp number; BEGIN temp := num; while (num > 0) loop fact := fact * num;

num := num - 1; END loop; DBMS_OUTPUT.PUT_LINE('factorial of ' || num ||' END; Check a no amstrong or not

is ' || fact);

DECLARE num number; tot number:=0; var1 number; var2 number; BEGIN num:=&num; tmp:=num; while tmp>0 loop var1:=tmp mod 10; tot:= tot + (var1*var1*var1); tmp:=tmp/10; END loop; if(tot==num) then DBMS_OUTPUT.PUT_LINE(num||' is armstrong no'); else DBMS_OUTPUT.PUT_LINE(num||' is not a armstrong no'); END if END; Print Fibonacci Series DECLARE num number := &n; n1 number := 0; n2 number := 1; n3 number; BEGIN DBMS_OUTPUT.PUT_LINE(n1); DBMS_OUTPUT.PUT_LINE(n2); for i in 3..num loop n3 := n1 + n2; DBMS_OUTPUT.PUT_LINE(n3); n1 := n2; n2 := n3; END loop; END; Prime number BEGIN for i in 1..100 loop if i in (1,5,7) then DBMS_OUTPUT.PUT_LINE(' These are known prime numbers '||i); END if; if i not in (1,5,7) then if mod(i,3)=0 or mod(i,6)=0 or mod(i,9)=0 then null; elsif mod(i,2)=0 or mod(i,4)=0 or mod(i,8)=0 then null; elsif mod(i,5)=0 or mod(i,10)=0 then null;

elsif mod(i,7)=0 then null; else DBMS_OUTPUT.PUT_LINE(' Is this a prime number?? '||i); END if; END if; END loop; END; How to use substitution variables DECLARE v_sal NUMBER(9,2) := &p_annual_sal; BEGIN v_sal := v_sal/12; DBMS_OUTPUT.PUT_LINE ('The monthly salary is ' ||TO_CHAR(v_sal)); END; / DEFINE p_annual_sal = 60000 Comosite type example DECLARE CURSOR myEmpCursor IS SELECT empno, ename FROM emp ORDER BY empno; myID emp.empno%TYPE; myName emp.ename%TYPE; BEGIN OPEN myEmpCursor; LOOP FETCH myEmpCursor INTO myID, myName; EXIT WHEN myEmpCursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(myID||' '||myName); END LOOP; CLOSE myEmpCursor; END Composite Rowtype example DECLARE e_name emp%rowtype; cursor emp_cur is select * from emp; BEGIN for i in emp_cur loop DBMS_OUTPUT.PUT_LINE(i.ename||','||i.job); END loop; END; Composite Record Type DECLARE TYPE emp_record_type is RECORD (name char(20), sal number(10), hiredate date); emp_rec emp_record_type; BEGIN select ename, sal,hire_date

into emp_record from Scott.emp where employee_id = 1047; DBMS_OUTPUT.PUT_LINE(emp_rec.name||' emp_rec.sal||' '|| emp_rec.hiredate); END; How to raise error DECLARE e_rec emp%ROWTYPE; e1 EXCEPTION; sal1 emp.sal%TYPE; BEGIN

'||

SELECT sal INTO sal1 FROM emp WHERE deptno = 30 AND ename = 'WARD'; IF sal1 < 5000 THEN RAISE e1; END IF; EXCEPTION WHEN no_data_found THEN RAISE_APPLICATION_ERROR (-20001, 'WARD is not there.'); WHEN e1 THEN RAISE_APPLICATION_ERROR (-20002, 'Less Salary.'); END; Using Cursor Select the Common deptno In Emp and Dept Table DECLARE CURSOR c_emp IS SELECT * FROM emp; cursor c_dept is select * from dept; BEGIN FOR i IN c_emp LOOP for j in c_dept loop if(i.deptno=j.deptno) then DBMS_OUTPUT.PUT_LINE(i.deptno); END if; END loop; END LOOP; END; Simple Cursor Example DECLARE CURSOR c_emp IS SELECT * FROM Scott.emp WHERE sal< 5000; BEGIN FOR i IN c_emp LOOP DBMS_OUTPUT.PUT_LINE(i.sal); insert into e1 values (i.sal); END LOOP; END;

Implecit Cursor Example DECLARE employee_id employee.emp_id%TYPE := 1; BEGIN DELETE FROM employee WHERE employee_id = employee_id; If(SQL%FOUND) DBMS_OUTPUT.PUT_LINE('Row Deleted'); END; /

Vous aimerez peut-être aussi