PL / SQL PROGRAMS

Last updated on Friday March 27, 17:51:58 GMT+0530 2009 PROGRAM BASED ON ASSIGNMENT STATEMENT :

P1. WRITE A NESTED PROGRAM TO ADD AND TO MULTIPLY TWO NUMBERS. DECLARE N1 number; N2 number; Sum number ; BEGIN Sum := N1+N2 ; << inner_block >> DECLARE Prod number; BEGIN Prod := N1 * N2 ; Dbms_output.put_line( Product Value = || prod ); END inner_block ; Dbms_output.put_line( Sum Value = || sum); END;

P2. WRITE A PROGRAM TO CALCULATE THE SIMPLE INTEREST AND COMPUND INTEREST , IF P, N, R ARE GIVEN. declare p number(9,2) ; n number(9,2) ; r number(9,2) ;

si number(9,2) := 0; ci number(9,2) := 0; begin p := &principal_amount; n := &no_of_years; r := &rate_of_interest; si := p*n*r/100; ci := p*(1+r/100)**n; dbms_output.put_line('simple interset =' ||si); dbms_output.put_line('compound interset =' ||ci); end; SQL> / Enter value for principal_amount: 10000 old 8: p:=&principal_amount; new 8: p:=10000; Enter value for no_of_years: 5 old 9: n:=&no_of_years; new 9: n:=5; Enter value for rate_of_interest: 10.5 old 10: r:=&rate_of_interest; new 10: r:=10.5; simple interset =5250 compound interset =16474.47 PL/SQL procedure successfully completed.

PROGRAM BASED ON IF LOOP P3 . Write a program to check greatest of two numbers : declare a number(3) :=20; b number(3) :=10; begin if a>b then dbms_output.put_line('a is the greatest : ' ||a); else dbms_output.put_line('B is the greatest : ' ||b); end if; end; SQL> / a is the greatest : 20 PL/SQL procedure successfully completed.

P4. Given 2 sides of a rectangle .Write a program to find out its area is greater than its perimeter or not .

declare l number; b number; ar number; pr number; begin l := &l; b := &b;

ar := l*b; pr := 2*(l+b); if ar > pr then dbms_output.put_line('the area iS > its perimeter'|| 'area = '||ar||'perimeter = '||pr); else dbms_output.put_line('the area iS < its perimeter'|| ' area = '||ar||' perimeter = '||pr); end if; end;

Enter value for l: 10 old 7: l:=&l; new 7: l:=10; Enter value for b: 6 old 8: b:=&b; new 8: b:=6; the area is > its perimeter area = 60 perimeter = 32 PL/SQL procedure successfully completed.

P5. WRITE A PROGRAM TO INPUT A SINGLE DIGIT NO: CONVERT IT INTO WORDS. Declare a number; t varchar2(10); begin a :=&a; if a=1 then

elsif a= then t := 'four'.t := 'one'. 2 = two . end if. new 5: a:=2. elsif a=5 then t := 'five'. elsif a=9 then t := 'nine'. elsif a=7 then t := 'seven'.put_line(a || ' = ' || t). else t := 'zero'. elsif a=8 then t := 'eight'. elsif a= 3 then t := 'three'. elsif a=2 then t := 'two'. dbms_output. elsif a=6 then t := 'six'. Enter value for a: 2 old 5: a:=&a. end.

begin if n>0 then dbms_output. declare a number(5). else dbms_output. ELSE DOUBLES IT. / SQL> save posneg.sql Created file posneg. WRITE A PROGRAM TO INPUT 2 NUMBERS IF THE 1st No >2nd No THEN SWAP IT. t number(5). end if.put_line('the given number is negative' || n). begin . end.sql SQL >The given number is positive 12 PL/SQL procedure successfully completed. b number(5). Write a program to check the given number is +ve or ve : SQL> declare n number(2):=12.put_line('the given number is positive' || n). P6 .PL/SQL procedure successfully completed. ELSE IF 1st No < 2nd No RAISE IT TO ITS POWER . PROGRAM BASED ON NESTED IF LOOP P7.

new 7: b:=5.put_line( 'initial value of a = '||a ||' b = '||b ) if a>b then t:= a. end. end if.put_line('final value of a = '||a ||' b = '||b). a:= b. SQL> / Enter value for a: 4 old 6: a:=&a. elsIF A<B then a:=a**a. ELSE a:=2*a. initial value of a = 4 b = 5 . dbms_output. b:=t. Enter value for b: 5 old 7: b:=&b. b := &b. dbms_output.a := &a. b:=2*b. new 6: a:=4. b:=b**b.

. new 6: a:=5. P8. initial value of a = 5 b = 5 final value of a = 10 b = 10 PL/SQL procedure successfully completed. new 7: b:=4. initial value of a = 5 b = 4 final value of a = 4 b = 5 PL/SQL procedure successfully completed. A bank accepts fixed deposits for one or more years and the policy it adopts on interest is as follows: If a deposit is < Rs 2000 and for 2 or more years . SQL> Enter value for a: 5 old 6: a:=&a. new 6: a:=5. SQL> / Enter value for a: 5 old 6: a:=&a. Enter value for b: 4 old 7: b:=&b.final value of a = 256 b = 3125 PL/SQL procedure successfully completed. the interest rate is 5% compounded annually. new 7: b:=5. Enter value for b: 5 old 7: b:=&b.

interest is 10% compounded annually. if p<2000 and t>=2 then r := 5. else .2). On all deposits for 5 years or more . begin p := &p. declare p number(9. r number(9. the interest is 3% compounded annually. the interest is 8 % compounded annually. elsif p>6000 and t>=1 then r := 8.If a deposit is Rs. Write a program to calculate the amount on maturity. t number(9.6000 and for 1 or more years . ci number(9. On all other deposits not covered by above conditions . the interest is 7 % compounded annually. If a deposit is Rs.2000 or more but less than 6000 and for 2 or more years . elsif t>=5 then r := 10. elsif p>=2000 and p<6000 and t>=2 then r := 7.2). t := &t.2).2). Given the amount deposited and the number of years .

Enter value for p: 1500 old 7: p:=&p.put_line('The compund interst is ='||ci). new 7: p:=10000.r := 3.put_line('The time period is ='||t).put_line('The rate of interest is ='||r). Enter value for t: 3 . dbms_output. dbms_output. Enter value for p: 10000 old 7: p:=&p. end.put_line('The Principal amount is ='||p). Enter value for t: 5 old 8: t:=&t. dbms_output. new 8: t:=5. dbms_output. end if.28 PL/SQL procedure successfully completed. The Principal amount is =10000 The rate of interest is =8 The time period is =5 The compund interst is =4693. ci := p*(1+r/100)**t .p. new 7: p:=1500.

if b>c then s:=b. new 8: t:=3. c :=&c. . b :=&b. WRITE A PROGRM TO INPUT 3 NUMBERS FIND THE 1st Greatest. if a>b and a>c then f:= a. s number(3). t number(3). 2nd Greatest.old 8: t:=&t. begin a :=&a.44 PL/SQL procedure successfully completed. c number(3). t:=c. The Principal amount is =1500 The rate of interest is =5 The time period is =3 The compound interest is =236. declare a number(3). 3rd Greatest. b number(3). P9. f number(3).

else f:= c. end if.put_line('third largest = ' ||t). dbms_output. t:=b. t:=b. t:=c.put_line('second largest = ' ||s). else s:=b.put_line('first largest = ' ||f). dbms_output.else s:=c. else s:=c. if a>b then s:=a. . dbms_output. end if. end if. elsif b>a and b>c then f:= b. end if. t:=a. t:=a. if a>c then s:=a.

end. op char(1) . b := &b . . Enter value for c: 9 old 11: c:=&c. declare a number(3) . new 11: c:=9. WRITE A PROGRAM TO INPUT 2 NUMBERS AND AN OPERATOR . Enter value for b: 8 old 10: b:=&b. new 9: a:=5. op := &op . c number(3) . begin a := &a . / Enter value for a: 5 old 9: a:=&a. first largest = 9 second largest = 8 third largest = 5 PL/SQL procedure successfully completed. new 10: b:=8. AND DISPLAY THE RESULT. b number(3) . P10 .

new 8: b:=6. end if. result=30 PL/SQL procedure successfully completed. new 9: op:='*'. Enter value for a: 5 old 7: a:=&a. dbms_output.put_line('result='||c).if op='+' then c:=a+b. else c:=a/b. elsif op='*' then c:=a*b. elsif op='-' then c:=a-b. . Enter value for op: '*' old 9: op:=&op. Enter value for b: 6 old 8: b:=&b. new 7: a:=5. end.

put_line(' the sales commission is :'||com). begin sman := &sman. If salesmade Comm >10000 500 10000 20000 1000 >20000 1500 declare sman varchar(10).put_line(' the sales made is :'||sm). Enter value for sman: 'Mathew' . dbms_output. if sm > 10000 then com := 500.put_line(' the sales man name is :'||sman). sm number(9.2). elsif sm > 20000 then com := 1000. com number(9. else com := 1500. end if. sm := &sm. dbms_output. dbms_output.2). end.P11. Write a program to calculate the commission of the sales man.

THE WHILE LOOP BEGINS 10 15 .old 6: sman:=&sman. TO GENERATE NUMBERS FROM 0 TO 25 IN STEP OF 5 declare i number :=10 . Enter value for sm: 15000 old 7: sm:=&sm. PROGRAM BASED ON WHILE LOOP P12. end loop.put_line(to_char(i)). begin dbms_output. WHILE I<=25 LOOP dbms_output. new 7: sm:=15000. The sales man name is :Mathew The sales made is :15000 The sales commission is :500 PL/SQL procedure successfully completed. new 6: sman:='Mathew'. End.put_line(' THE WHILE LOOP begins'). i:=i+5.

WHILE N<>0 LOOP R := MOD(N.20 25 PL/SQL procedure successfully completed.put_line('THE SUM OF THE DIGITS = ' || S). PROGRAM BASED ON NESTED WHILE LOOP . end. R NUMBER. dbms_output. new 7: n:=375.10). SQL> Enter value for n: 375 old 7: n:=&N. P13. N := TRUNC(N/10). Write a program to find the sum of the digits of the number: DECLARE N number . end loop. begin n:=&N. S NUMBER :=0. S := S + R. THE SUM OF THE DIGITS = 15 PL/SQL procedure successfully completed.

3 LOOP dbms_output. p15.. n/2 loop v := i*2. declare i number(5). end loop.. s number(5):=0.put_line('the sum of numbers from 2 to '||n||' = ' ||s).PROGRAM BASED ON FOR LOOP P14.put_line(t||' X '|| i || ' = ' ||i*t ). end loop. s := s+v. begin T := &T. write aprogram to generate even numbers from 2 to 50. v number(5). dbms_output. and find its sum. for i in 1 . WRITE A PROGRAM CODE TO PRINT THE MULTIPLICATION TABLE OF A GIVEN NO: declare t number(3) := 3. . FOR I IN 1. end. n number(5). dbms_output. begin n := &terminal_number.put_line(v).

2 4 6 8 10 The sum of numbers from 2 to 10 = 30 PL/SQL procedure successfully completed. c number. declare a number:= 0 . b := c.end. new 7: n:=10. P16 .10 loop c := a + b. a := b. begin dbms_output. SQL> / Enter value for terminal_number: 10 old 7: n:=&terminal_number. . WRITE A PROGRAM TO GENERATE FIRST 25 TERMS OF THE FIBONACCIS SERIES. for i in 3.. dbms_output.put(c||' ').put(a||' '||b||' '). b number:= 1.

. P17. begin n :=&n.end loop. new 6: n:=5. end loop. for i in 1.put_line(' '). i number(2). dbms_output.n loop f := f * i. f number(5):=1. end. end. Write a program to find the factorial of a number : declare n number(2). dbms_output.put_line(' the factorial value = '||f). Enter value for n: 5 old 6: n:=&n. 0 1 1 2 3 5 8 13 21 34 PL/SQL procedure successfully completed. the factorial value = 120 PROGRAM BASED ON NESTED FOR LOOP .

dbms_output. for i in 1.P18. begin n :=&n. new 6: n:=5. WRITE A PROGRAM TO DISPLAY NUMBERS OF THE FORM 00000 12345 2 4 6 8 10 . end loop. n number. end. P19..put_line(' ').. Enter value for n: 5 old 6: n:=&n. end loop.put(j). Write a program to print the following design: 1 12 123 1234 12345 declare i number .n loop for j in 1.i loop dbms_output. j number.

put_line('THE GIVEN TEXT :'||G).5 LOOP K := I*J. PUT_LINE (' '). J NUMBER . .1). END LOOP. END LOOP. 5 LOOP FOR J IN 1. BEGIN FOR I IN 0 . CHECK THE TEXT IS PALINDROME OR NOT DECLARE G VARchar2(20). K NUMBER..3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 DECLARE I NUMBER. for i in REVERSE 1. WRITE A PL/SQL CODE TO ACCEPT THE TEXT AND REVERSE THE GIVEN TEXT.i. DBMS_OUTPUT. r VARchar2(20).. dbms_output. DBMS_OUTPUT. P20.PUT(K).. END. BEGIN G:='&g'. length(G) loop R:= R||substr(G.

END IF. ELSE dbms_output. Write a program to print the following design: declare .put_line('THE GIVEN TEXT IS PALINDROME '). / Enter value for g: HELLO old 5: G:='&g'.end loop. THE GIVEN TEXT :HELLO THE REVERSED TEXT :OLLEH THE GIVEN TEXT IS NOT PALINDROME PL/SQL procedure successfully completed. new 5: G:='MALAYALAM'. end. IF R=G THEN dbms_output. SQL> / Enter value for g: MALAYALAM old 5: G:='&g'. new 5: G:='HELLO'. THE GIVEN TEXT :MALAYALAM THE REVERSED TEXT :MALAYALAM THE GIVEN TEXT IS PALINDROME PL/SQL procedure successfully completed.put_line('THE GIVEN TEXT IS NOT PALINDROME '). P21. dbms_output.put_line('THE REVERSED TEXT :'||R).

k number. end loop. ~~~~1 . end loop.n loop for j in 1.put('~').n-i loop dbms_output.. begin n := &n. new 8: n:=5. end loop.i number . for k in reverse 1. m number. n number..i loop dbms_output.put(k). Enter value for n: 5 old 8: n:=&n. j number. dbms_output..put(k). for i in 1. end loop. for k in 1. end.i-1 loop dbms_output..put_line(' ').

k number. n number. dbms_output. j number.put('~'). begin n := &n..put(k). end loop.n-i loop dbms_output. for k in reverse 1.put_line(' '). for i in 1... P22. end loop.. end loop. for k in 1.put(k). m number.n loop for j in 1.~~~121 ~~12321 ~1234321 123454321 PL/SQL procedure successfully completed. .i loop dbms_output.i-1 loop dbms_output. Write a program to print the following design : declare i number .

i-1 loop dbms_output. new 8: n:=5.end loop. end loop. ~~~~1 ~~~121 ~~12321 ~1234321 123454321 ~1234321 ~~12321 ~~~121 ~~~~1 .n-i loop dbms_output.. end loop.put('~').put_line(' '). for i in reverse 1. for k in reverse 1...put(k).n-1 loop for j in 1. end.put(k). end loop. dbms_output. Enter value for n: 5 old 8: n:=&n. for k in 1.i loop dbms_output. end loop. .

DECLARE I NUMBER(4). PROGRAM BASED ON FOR & IF P23. SQL>/ 1 3 5 7 9 THE SUM OF ODD NOS FROM 1 TO 10 = 25 . END IF. END. DBMS_OUTPUT.10 LOOP IF MOD(I.PUT_LINE(' THE SUM OF ODD NOS FROM 1 TO 10 = ' ||S). BEGIN FOR I IN 1.PL/SQL procedure successfully completed. S NUMBER(4):=0. DBMS_OUTPUT..2) <> 0 THEN S := S+I.PUT_LINE(I). GENERATE ODD NOS: FROM 1 TO 10 AND FIND ITS SUM. END LOOP.

2) <>0 THEN s := s + I.put_line('the sum of numbers from 2 to '||n||' = ' ||s). Enter value for starting number : 1 Old 6: s=&starting_number:1 New 6: s:=1. end. Enter value for Terminal_number: 10 old 7: n := &terminal_number. dbms_output.. 9 7 . new 7: n:=10. n := &terminal_number. dbms_output.put_line(I).PL/SQL procedure successfully completed. end loop. n number(5). for i in REVERSE S . N loop IF MOD (I. s number(5) :=0. v number(5). WRITE A PROGRAM TO ALL ODD NUMBERS FORM 10 TO 1 IN REVERSE ORDER. P24 . declare i number(5). begin S := &STARTING_NUMBER.

i number. Enter value for n: 7 old 6: n:=&n. end loop. .put_line(' the given no: is prime: '||n).i) = 0 then pr := 0. end. Write a program to check the given no: is prime or not: declare n number. begin n:= &n. end if. else dbms_output. if pr = 1 then dbms_output. end if.5 3 1 The sum of numbers from 1 to 9 = 25 PL / SQL procedure successfully completed.n/2 loop if mod(n. pr number(2):=1. for i in 2..put_line(' the given no: is not prime: '||n). new 6: n:=7. P25.

046:.256 LOOP DBMS_OUTPUT. 033:! 034:" 035:# 036:$ 037:% 038:& 039:' 040:( 041:) 042:* 043:+ 044:.. 047:/ 048:0 049:1 050:2 051:3 052:4 053:5 054:6 055:7 056:8 249:ù 250:ú 251:û 252:ü 253:ý 254:þ 255:ÿ 256: :: :: :: :: :: :: :: :: :: :: :: :: PL/SQL procedure successfully completed.PUT_LINE(' ').'000')) || ':' || CHR(I) || ' ' ). the given no: is not prime: 25 PL/SQL procedure successfully completed. END.PUT (( TO_CHAR (I. P26. SQL> / Enter value for n: 25 old 6: n:=&n. END LOOP. WRITE A PROGRAM TO PRINT ASCII TABLE : DECLARE I NUMBER. . BEGIN FOR I IN 33. END IF. 045: .the given no: is prime: 7 PL/SQL procedure successfully completed. IF MOD (I. new 6: n:=25. 8) = 0 THEN DBMS_OUTPUT.

n/2 loop if mod(n. Write a program to generate prime nos from 1 to 10: declare n number. begin for n in 1. end if.P27. if pr = 1 then dbms_output. 1 2 3 5 7 P28 .10 loop pr:=1..put_line(n). declare . end loop. i number.. end loop. end if.i) = 0 then pr := 0. Write a program to check the square root of a number is prime or not. pr number. for i in 2 . end.

end loop. i number. SQL> Enter value for n: 81 old 7: n:=&n..put_line('the square root of the given number is prime'||n*n). for i in 2 . PROGRAM BASED ON LOOP. n := sqrt(n). pr number. end.. The square root of the given number is not prime81 PL/SQL procedure successfully completed.put_line('the square root of the given number is not prime'||n*n).i) = 0 then pr := 0. n/2 loop if mod(n. Write a program to reverse the digits of the number: . end if.n number. if pr = 1 then dbms_output. new 7: n:=81. END LOOP P29.. end if. begin pr := 1. n := &n. else dbms_output.

N := TRUNC(N/10). Enter value for n: 4567 old 7: N:=&N. Structure of the table : . end. S := S + R. PROGRAM BASED ON RECORDS P30. WRITE PL/SQL SCRIPT TO CREATE A RECORD TYPE THAT CAN HOLD SALES IN 4 DIFFERENT QUARTERS. R NUMBER. dbms_output.put_line( ' THE REVERSED DIGITS ' || ' OF ' || K || ' = ' || S). K number. end loop. S NUMBER : = 0. R := MOD(N.10). begin N := &N. LOOP EXIT WHEN N = 0 .DECLARE N number . new 7: N:=4567. THE REVERSED DIGITS OF 4567 = 7654 PL/SQL procedure successfully completed. S := S * 10. K := N.

PROGRAM BASED ON DATABASE INTERACTION P31 . QUAD4 ) SALES2004 ( Q1.EMPNO%TYPE. SREC. SALARY EMP. QUAD1. . Write PL/SQL SCRIPT to determine the salary of employee in the EMP table. SREC.Q3. QUAD2. Q2 NUMBER. Q4 ) DECLARE TYPE SALEREC IS RECORD ( Q1 NUMBER. Q3 NUMBER.Q4). NAME. If salary >7500 give an increment of 15% .SALES ( SNO. BEGIN SREC SALEREC . ).Q2.Q1 . Q4 NUMBER. END.SAL%TYPE. QUAD3. SUM(QUAD2) . INSERT INTO SALES2004 VALUES (SREC. BEGIN ENO := &ENO. Get the empno from the user.SREC. otherwise display the message NO INCREMENT . SUM(QUAD4) INTO SREC FROM SALES . Q2. Q3. SUM(QUAD3). SELECT SUM(QUAD1) . DECLARE ENO EMP.

SID%TYPE. GPA FLOAT ).SELECT SAL INTO SALARY FROM EMP WHERE EMPNO=ENO. LOOP EXIT WHEN TSID > 857 . END.0 WHERE SID=TSID.0 THEN UPDATE STUDENT SET GPA=4. END IF. TSID:=TSID+1. IF TGPA< 4.GPA%TYPE. NAME CHAR(30). BEGIN TSID:=142. WRITE A PL/SQL SCRIPT TO GO THROUGH SID 142 -857 AND SET ALL GPA UNDER 4.0 . TGPA STUDENT. IF SALARY >7500 THEN UPDATE EMP SET SAL =SAL+SAL* 15/100 WHERE EMPNO=ENO. . GIVEN A TABLE STUDENT ( SID INTEGER PRIMARY KEY. SELECT GPA INTO TGPA FROM STUDENT WHERE SID=TSID. END. ELSE DBMS_OUTPUT.0 TO 4.PUT_LINE(NO INCREMENT) END IF. DECLARE TSID STUDENT. P32 . END LOOP. AGE INTEGER .

DBMS_OUTPUT.P33 . SSAL EMP. DNO := DNO+10.PUT_LINE( AVERAGE SALARY || ASAL). IF TOTAL <10 DISPLAY SMALL COMPANY. ELSE IF TOTAL BETWEEN 10 -50 DISPLAY MEDIUM ELSE DISPLAY LARGE .20.SSAL FROM EMP WHERE DEPTNO = DNO. SELECT DNAME INTO DPNAME FROM DEPT WHERE DEPTNO=DNO. DECLARE DNO NUMBER:=10. ACOUNT NUMBER. TOTOL NO: OF EMP.30. DNAME%TYPE. DBMS_OUTPUT.5090. BEGIN LOOP EXIT WHEN DNO > 50.PUT_LINE( SUM SALARY || SSAL). DBMS_OUTPUT.PUT_LINE( DEPARTMENT COUNT || ACOUNT). AND TOTAL SALARY FOR FIRST 5 DEPARTMENTS IN EMP TABLE.PUT_LINE( DEPARTMENT NAME || DPNAME). COUNT(*).40. DECLARE . SUM(SAL) INTO ASAL. END. ACOUNT. SAL%TYPE. DPNAME DEPT. DBMS_OUTPUT. WRITE A PL/SQL SCRIPT TO COUNT THE NO: OF EMPLOYEES. ASAL EMP. END LOOP. P34. DISPLAY AVERAGE SALARY . DEPT NO ARE 10. SAL%TYPE. SELECT AVG(SAL).

DBMS_OUTPUT. ELSIF TOT<50 THEN TEXT := MEDIUM. ENAME. Find the employee drawing minimum salary. SALARY := SALARY+700.SAL%TYPE.ENAME%TYPE. sal into the table newsal. BEGIN SELECT COUNT(*) INTO TOT FROM EMP. SAL INTO ENO. SALARY FROM EMP WHERE SAL = (SELECT MIN(SAL) FROM EMP).NAME.SALARY). END. BEGIN SELECT EMPNO. SALARY EMP. NAME EMP. DECLARE ENO EMP. INSERT INTO NEWSAL VALUES ( ENO. NAME. END IF. Add his details like empno.PUT_LINE( TEXT || COMPANY). name. IF TOT <10 THEN TEXT := SMALL . . ELSE TEXT := LARGE. END. after incrementing Rs.700.TOT NUMBER (3).EMPNO%TYPE. P35.

END IF. DNO NUMBER. GIVEN A TABLE TEMP ( SNO NUMBER(3) . Get the empno from the user. SELECT SAL INTO S FROM EMP WHERE ENAME = NAME. WRITE A PL/SQL SCRIPT TO INSERT 10 RECORDS IN TABLE TEMP AS PER THE FOLLOWING SPECIFICATIONS: . DECLARE ENO NUMBER(4). END. ELSE UPDATE EMP SET SAL := SAL+SAL*15/100 WHERE ENAME=NAME. BEGIN ENO := &ENO.Write PL/SQL code to increase the sal of an employee by 5% whose salary is more than 4000 . NAME VARCHAR2(10). Write a program to update the salary of the employee by 25% if he earns salary >4000. ELSIF S<4000 UPDATE EMP SET SAL := SAL+SAL*10/100 WHERE ENAME=NAME. else update by 15%.2) . P38. DECLARE S NUMBER(8. BEGIN NAME:=&NAME. otherwise if salary <4000 update by 10%. IF S >4000 THEN UPDATE EMP SET SAL := SAL+SAL*20/100 WHERE ENAME=NAME.P36.TEXT CHAR (4) . P37 . END. UPDATE EMP SET SAL =SAL+SAL*5/100 WHERE SAL>4000 AND EMPNO=ENO.

2 ) = 0 TEXT := EVEN. ELSE TEXT := ODD. DNO := SNO * 50.SNO DNO TEXT 1 50 ODD 2 100 EVEN 3 150 ODD DECLARE SNO NUMBER := 1. DECLARE . P39 . Get the empno from the user. IF MOD ( SNO. Write PL/SQL code to DELETE the record of an employee whose salary is more than 4000 . SNO := SNO + 1. BEGIN LOOP EXIT WHEN SNO > 10. TEXT CHAR(5) . COMMIT. END LOOP. DNO NUMBER .TEXT).DNO. END. INSERT INTO TEMP VALUES (SNO.

insert into emp (empno. END.ename. P40 . thiredate :='&thiredate'.sal) values(tempno.'DD-monyyyy'). Enter value for tempno: 8000 old 7: tempno:=&tempno.to_date(thiredate.ENO NUMBER(4). begin tempno :=&tempno.tsal). BEGIN ENO := &ENO.Write PL/SQL code to insert a new record in the table emp after obtaining values ( empno. declare tempno number(4). sal ) from user. DELETE FROM EMP WHERE SAL>4000 AND EMPNO=ENO. Enter value for tename: mathew old 8: tename :='&tename'. ename. end. new 8: tename :='mathew'. new 7: tempno:=8000. .2).hiredate. tsal := &tsal. tename varchar(10). tename :='&tename'. hiredate.tename. tsal number(7. thiredate varchar2(12).

empno%type.empno) .deptno). AND DISPLAY ALL THE INFORMATION ABOUT THE EMPLOYEE. dbms_output.put_line( 'Deptno : '||erec. new 10: tsal := 8000.put_line( 'job : '||erec. declare erec emp%rowtype. PL/SQL procedure successfully completed.job).sal). P41. Enter value for tsal: 8000 old 10: tsal := &tsal. select * into erec from emp where empno=eno. new 9: thiredate :='10-jan-2004'.ename) . begin eno := &eno. dbms_output.put_line( 'Emp no : '||erec.Enter value for thiredate: 10-jan-2004 old 9: thiredate :='&thiredate'. dbms_output. dbms_output.put_line( 'hiredate: '||erec. end.put_line( 'Name : '||erec.put_line( 'Salary : '||erec.ACCEPT THE EMPNO FROM THE USER. dbms_output. WRITE A PROGRAM TO CREATE A EMP %ROWTYPE RECORD . dbms_output. SQL> / Enter value for eno: 7788 . eno emp.hiredate).

old 5: eno := &eno.ENMAE%TYPE. SNO VARCHAR2(5). DBMS_OUTPUT. SELECT EMPNO. NAME EMP. DECLARE ENO EMP. new 5: eno := 7788.PUT_LINE(SNO||IS INVALID DATA FOR EMPLOYEE ID).PUT_LINE ( NAME || ENAME || EMPNO ||EMPNO).NAME FROM EMP WHERE EMPNO=SNO.ENAME INTO ENO. Write PL/SQL script that traps an invalid data type value given and displays a custom error message. EXCEPTION WHEN INVALID_NUMBER THEN DBMS_OUTPUT. BEGIN SNO := &SNO . PROGRAM BASED ON EXCEPTION P42. EMPNO%TYPE. END. Emp no : 7788 Name : SCOTT Salary : 1452 Deptno : 20 hiredate: 19-APR-87 job : ANALYST PL/SQL procedure successfully completed. .

--------. N LOOP F:= F*I. SQL> select fact(8) from dual.PROGRAM BASED ON FUNCTIONS P43 .--------. CREATE OR REPLACE FUNCTION FACT (N NUMBER) RETURN NUMBER IS I NUMBER(10).--------- .---------. END. FACT(8) --------40320 PROGRAM BASED ON PROCEDURES P44..--------.--------. Write a Procedure to increase the salary for all the employees in the EMP table : SQL> select * from emp. END LOOP. F NUMBER :=1. write a function to create factorial of a number. BEGIN FOR I IN 1. SQL> / Function created. RETURN F.--------. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------.

/ Procedure created. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1700 300 30 14 rows selected. SQL> select * from emp.7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 14 rows selected. SQL> create or replace procedure inc(i number) is begin update emp set sal =sal+i. end. P45. / PL/SQL procedure successfully completed. end. To execute the procedures in the PL/SQL block: SQL> declare begin inc(100). WRITE A PROCEDURE TO INCREASE THE SALARY FOR THE SPECIFIED EMPLOLEE .

UPDATE EMP SET SAL=SAL+SAL*7/100 WHERE JOB='SALESMAN' AND EMPNO=ENO. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPT 7369 SMITH CLERK 7902 17-DEC-80 840 20 .USING EMPNO IN THE EMP TABLE BASED ON THE FOLLOWING CRITERIA: INCREASE THE SALARY BY 5% FOR CLERKS. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPT 7369 SMITH CLERK 7902 17-DEC-80 800 20 CREATE OR REPLACE PROCEDURE DESIGNATION(ENO NUMBER) IS BEGIN UPDATE EMP SET SAL=SAL+SAL*5/100 WHERE JOB ='CLERK' AND EMPNO=ENO. 4 END. UPDATE EMP SET SAL=SAL+SAL*25/100 WHERE JOB='PRESIDENT' AND EMPNO=ENO. 20 % FOR MANAGER and 25% FOR PRESIDENT. SQL> SELECT * FROM EMP. 7% FOR SALESMAN . END. SQL> / Procedure created. 10% FOR ANALYST. UPDATE EMP SET SAL=SAL+SAL*20/100 WHERE JOB='MANAGER' AND EMPNO=ENO. ACTIVATE USING PL/SQL BLOCK. UPDATE EMP SET SAL=SAL+SAL *10/100 WHERE JOB='ANALYST' AND EMPNO=ENO. 5/ PL/SQL procedure successfully completed. SQL> DECLARE 2 BEGIN 3 DESIGNATION(7369). SQL> SELECT * FROM EMP.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.