Plsqlpgrms
Plsqlpgrms
Plsqlpgrms
Introduction(Basic Program):
OUTPUT:
HAI
WELCOME
PL/SQL PROGRAMS
SQL>
DECLARE
A NUMBER;
B NUMBER;
C NUMBER;
BEGIN
A:=&A;
B:=&B;
C:=A+B;
DBMS_OUTPUT.PUT_LINE('THE SUM OF TWO INTEGERS IS: '||C);
END;
OUTPUT:
Enter value for a: 1
old 6: A:=&A;
new 6: A:=1;
Enter value for b: 2
old 7: B:=&B;
new 7: B:=2;
THE SUM OF TWO INTEGERS IS: 3
SQL>
DECLARE
EMPNO NUMBER;
ENAME VARCHAR2(20);
SAL NUMBER(7,2);
ANU_SAL NUMBER(10,2);
BONUS NUMBER(10,2);
BEGIN
EMPNO:=&EMPNO;
ENAME:='&ENAME';
SAL:=&SAL;
ANU_SAL:=SAL*12;
BONUS:=ANU_SAL*20/100;
DBMS_OUTPUT.PUT_LINE('EMPNO: '||EMPNO);
DBMS_OUTPUT.PUT_LINE('ENAME: '||ENAME);
DBMS_OUTPUT.PUT_LINE('SAL: '||SAL);
DBMS_OUTPUT.PUT_LINE('BONUS: '||BONUS);
END;
OUTPUT:
Enter value for empno: 10
old 8: EMPNO:=&EMPNO;
new 8: EMPNO:=10;
Enter value for ename: BALU
old 9: ENAME:='&ENAME';
new 9: ENAME:='BALU';
Enter value for sal: 15000
old 10: SAL:=&SAL;
new 10: SAL:=15000;
EMPNO: 10
ENAME: BALU
SAL: 15000
BONUS: 36000
SQL>
DECLARE
PRODNO NUMBER;
PNAME VARCHAR2(20);
QUAN NUMBER(3);
PRICE NUMBER(7,2);
TOTAL NUMBER(7,2);
DISCOUNT NUMBER(7,2);
NET NUMBER(7,2);
BEGIN
PRODNO:=&PRODNO;
PNAME:='&PNAME';
QUAN:=&QUAN;
PRICE:=&PRICE;
TOTAL:=QUAN*PRICE;
DISCOUNT:=TOTAL*20/100;
NET:=TOTAL-DISCOUNT;
DBMS_OUTPUT.PUT_LINE('PRODNO: '||PRODNO);
DBMS_OUTPUT.PUT_LINE('PNAME: '||PNAME);
DBMS_OUTPUT.PUT_LINE('QUANTITY: '||QUAN);
DBMS_OUTPUT.PUT_LINE('PRICE: '||PRICE);
DBMS_OUTPUT.PUT_LINE('TOTAL: '||TOTAL);
DBMS_OUTPUT.PUT_LINE('DISCOUNT: '||DISCOUNT);
DBMS_OUTPUT.PUT_LINE('NET BALANCE: '||NET);
END;
OUTPUT:
Enter value for prodno: 110
old 10: PRODNO:=&PRODNO;
new 10: PRODNO:=110;
Enter value for pname: PEN
old 11: PNAME:='&PNAME';
new 11: PNAME:='PEN';
Enter value for quan: 12
old 12: QUAN:=&QUAN;
new 12: QUAN:=12;
Enter value for price: 10
old 13: PRICE:=&PRICE;
new 13: PRICE:=10;
PRODNO: 110
PNAME: PEN
QUANTITY: 12
PRICE: 10
TOTAL: 120
DISCOUNT: 24
NET BALANCE: 96
SQL>
DECLARE
EMPNO NUMBER;
SAL NUMBER(7,2);
ANU_SAL NUMBER(7,2);
BONUS NUMBER(7,2);
BEGIN
EMPNO:=&EMPNO;
SAL:=&SAL;
ANU_SAL:=SAL*12;
IF SAL>=10000 THEN
BONUS:=ANU_SAL*20/100;
ELSIF SAL>=5000 AND SAL<10000 THEN
BONUS:=ANU_SAL*15/100;
ELSIF SAL>=3000 AND SAL<5000 THEN
BONUS:=ANU_SAL*12/100;
ELSIF SAL>=1500 AND SAL<3000 THEN
BONUS:=ANU_SAL*10/100;
ELSE
BONUS:=ANU_SAL*8/100;
END IF;
DBMS_OUTPUT.PUT_LINE('EMPNO: '||EMPNO);
DBMS_OUTPUT.PUT_LINE('SAL: '||SAL);
DBMS_OUTPUT.PUT_LINE('ANU_SAL: '||ANU_SAL);
DBMS_OUTPUT.PUT_LINE('BONUS: '||BONUS);
END;
OUTPUT:
Enter value for empno: 10
old 7: EMPNO:=&EMPNO;
new 7: EMPNO:=10;
Enter value for sal: 1000
old 8: SAL:=&SAL;
new 8: SAL:=1000;
EMPNO: 10
SAL: 1000
ANU_SAL: 12000
BONUS: 960
PL/SQL procedure successfully completed.
b)Write a Program to print numbers from 10-1.
SQL>
DECLARE
I NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('THE NUMBERS ARE');
FOR I IN REVERSE 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(I);
END LOOP;
END;
OUTPUT:
THE NUMBERS ARE
10
9
8
7
6
5
4
3
2
1
c) Write a Program to accept a date & print next 7 days along with day.
SQL>
DECLARE
DA DATE;
I NUMBER;
BEGIN
DA:='&DA';
FOR I IN 1..7 LOOP
DBMS_OUTPUT.PUT_LINE('THE DATE IS:'||DA);
DA:=DA+1;
END LOOP;
END;
OUTPUT:
Enter value for da: 10-JAN-10
old 5: DA:='&DA';
new 5: DA:='10-JAN-10';
THE DATE IS:10-JAN-10
THE DATE IS:11-JAN-10
THE DATE IS:12-JAN-10
THE DATE IS:13-JAN-10
THE DATE IS:14-JAN-10
THE DATE IS:15-JAN-10
THE DATE IS:16-JAN-10
PL/SQL procedure successfully completed.
d)Write a Program to display dept details
SQL>
DECLARE
CURSOR EC IS SELECT * FROM DEPT;
BEGIN
FOR V_EC IN EC
LOOP
DBMS_OUTPUT.PUT_LINE('DEPTNO='||V_EC.DEPTNO);
DBMS_OUTPUT.PUT_LINE('DNAME='||V_EC.DNAME);
DBMS_OUTPUT.PUT_LINE('LOC='||V_EC.LOC);
END LOOP;
END;
OUTPUT:
deptno=10
dname=ACCOUNTING
loc=NEW YORK
deptno=20
dname=RESEARCH
loc=DALLAS
deptno=30
dname=SALES
loc=CHICAGO
deptno=40
dname=OPERATIONS
loc=BOSTON
e)Write a Program to increment all emp sal by 10% who are working in grade 2&3.
SQL>
DECLARE
CURSOR EC IS SELECT E.EMPNO,E.ENAME,E.SAL,S.GRADE FROM EMP
E,SALGRADE S WHERE S.GRADE IN(2,3) AND E.SAL BETWEEN S.LOSAL AND
S.HISAL;
BEGIN
FOR V_EC IN EC
LOOP
UPDATE EMP SET SAL=V_EC.SAL+V_EC.SAL*0.1 WHERE EMPNO=V_EC.EMPNO;
END LOOP;
END;
OUTPUT:
Before :
After:
SQL>
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
V_EC C1%ROWTYPE;
ANN_SAL NUMBER(8,2);
EXP NUMBER(8,2);
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO V_EC;
EXIT WHEN C1%NOTFOUND;
ANN_SAL := V_EC.SAL*12;
EXP := MONTHS_BETWEEN(SYSDATE,V_EC.HIREDATE)/12;
DBMS_OUTPUT.PUT_LINE('EMPNO=' ||V_EC.EMPNO);
DBMS_OUTPUT.PUT_LINE('ANN_SAL=' ||ANN_SAL);
DBMS_OUTPUT.PUT_LINE('EXP=' ||EXP);
END LOOP;
CLOSE C1;
END;
OUTPUT:
empno=7839
ann_sal=60000
exp=30.37
empno=7698
ann_sal=34200
exp=30.91
empno=7782
ann_sal=29400
exp=30.81
empno=7566
ann_sal=35700
exp=30.99
empno=7654
ann_sal=15000
exp=30.5
empno=7499
ann_sal=19200
exp=31.11
empno=7844
ann_sal=18000
exp=30.56
empno=7900
ann_sal=11400
exp=30.32
empno=7521
ann_sal=15000
exp=31.1
empno=7902
ann_sal=36000
exp=30.32
empno=7369
ann_sal=9600
exp=31.28
empno=7788
ann_sal=36000
exp=29.31
empno=7876
ann_sal=13200
exp=29.21
empno=7934
ann_sal=15600
exp=30.18
b)Write a Program to calc bonus for all emps insert into bonus table
SQL>
DECLARE
CURSOR EC IS SELECT EMPNO,SAL FROM EMP;
V_EC EC%ROWTYPE;
ANN_SAL NUMBER(10,2);
B BONUS1%ROWTYPE;
BEGIN
OPEN EC;
LOOP
FETCH EC INTO V_EC;
EXIT WHEN EC%NOTFOUND;
ANN_SAL :=V_EC.SAL*12;
B.BONUS_AMT := ANN_SAL*0.2;
INSERT INTO BONUS1(EMPNO,BONUS_AMT,ADD_AMT,ISS_DATE)
VALUES(V_EC.EMPNO,B.BONUS_AMT,1000,SYSDATE
END LOOP;
CLOSE EC;
END;
OUTPUT:
SQL>
DECLARE
CURSOR EC IS SELECT E.EMPNO,E.JOB,E.SAL,D.DEPTNO,D.LOC,S.GRADE FROM
EMP E,SALGRADE S,DEPT D WHERE E.JOB='MANAGER' AND
E.DEPTNO=D.DEPTNO AND E.SAL BETWEEN S.LOSAL AND S.HISAL;
V_EC EC%ROWTYPE;
BEGIN
OPEN EC;
LOOP
FETCH EC INTO V_EC;
EXIT WHEN EC%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_EC.EMPNO);
DBMS_OUTPUT.PUT_LINE(V_EC.JOB);
DBMS_OUTPUT.PUT_LINE(V_EC.SAL);
DBMS_OUTPUT.PUT_LINE(V_EC.DEPTNO);
DBMS_OUTPUT.PUT_LINE(V_EC.LOC);
DBMS_OUTPUT.PUT_LINE(V_EC.GRADE);
END LOOP;
CLOSE EC;
END;
OUTPUT:
7698
MANAGER
2850
30
CHICAGO
4
7782
MANAGER
2450
10
NEW YORK
4
7566
MANAGER
2975
20
DALLAS
4
sql>
DECLARE
a number;
b number;
c number;
CREATE OR REPLACE PROCEDURE min(x IN number, y IN number, z OUT
number)
IS
BEGIN
IF x<y THEN
z:=x;
ELSE
z:=y;
END IF;
END;
BEGIN
a:=25;
b:=40;
minimum(a,b,c);
dbms_output.put_line(c);
END;
Output:
40
Sql>
sql>
DECLARE
a number;
b number;
c number;
FUNCTION max(x in number, y in number)
RETURN number
IS
z number;
BEGIN
IF x>y THEN
z:=x;
ELSE
z:=y;
END IF;
RETURN z;
END Max;
BEGIN
a:=20;
b:=40;
c:=max(a, b);
dbms_output.put_line(‘Maximum of (20, 40):’||c);
END;
Output:
Maximum of (20, 40): 40
DECLARE
x number;
y number;
z number;
FUNCTION palindrome(a in out number)
RETURN number IS
temp number;
rem number;
BEGIN
temp:=0;
m:=n;
WHILE(n>0)
LOOP
rem:=mod(n, 10);
temp:=(temp*10)+rem;
n:=n/10;
END LOOP;
RETURN m;
END;
BEGIN
x:=1234;
y:=x;
z:=palindrome(x)
IF (z=y) THEN
dbms_output.put_line(‘palindrome);
ELSE
dbms_output.put_line(‘not palindrome’);
END IF;
END;
/
Output:
statement processed
palindrome