Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CURSOR EXPERIMENT:
- 0. Program 0: Program TO access tables
- DECLARE
- job_count NUMBER;
- emp_count NUMBER;
- BEGIN
- SELECT COUNT(DISTINCT id) INTO job_count FROM emp22;
- SELECT COUNT(*) INTO emp_count FROM emp22;
- DBMS_OUTPUT.put_line('job COUNT='||job_count);
- DBMS_OUTPUT.put_line('employee COUNT='||emp_count);
- END;
- /
- 1. Program 01:
- DECLARE
- BEGIN
- FOR emp IN(SELECT id,name FROM emp22)
- LOOP
- DBMS_OUTPUT.put_line('Employee id AND employee name are '||emp.id||'AND '||emp.name);
- END LOOP;
- END;
- /
- 2. Program 02:
- DECLARE
- CURSOR emp2 IS SELECT id,name,salary FROM emp22 WHERE role=’manager’;
- BEGIN
- FOR emrec IN emp2
- LOOP
- UPDATE emp22 SET salary=emrec.salary+20000 WHERE id=emrec.id;
- END LOOP;
- END;
- /
- 3. Program 03:
- DECLARE
- CURSOR cur1 IS SELECT id,salary FROM emp22 WHERE id=102;
- ecode emp22.id % TYPE;
- esal emp.salary % TYPE;
- BEGIN
- OPEN cur1;
- LOOP
- FETCH cur1 INTO ecode,esal;
- EXIT WHEN cur1 % notFound;
- DBMS_OUTPUT.put_line('Employee code AND salary are '||ecode||'AND '||esal);
- END LOOP;
- CLOSE cur1;
- END;
- /
- 4. Program 04:
- DECLARE
- CURSOR cur1 IS SELECT id,salary FROM emp22 WHERE salary=60000;
- ecode emp22.id % TYPE;
- esal emp.salary % TYPE;
- BEGIN
- OPEN cur1;
- LOOP
- FETCH cur1 INTO ecode,esal;
- EXIT WHEN cur1 % notFound;
- DBMS_OUTPUT.put_line('Employee code AND salary are '||ecode||'AND '||esal);
- END LOOP;
- CLOSE cur1;
- END;
- /
- ----------------------------------------------------------------------------------------------------------------------------
- Complete the following list OF simple programs IN PL/SQL - PREVIOUS EXPERIMENTS
- 1. Addition OF two numbers
- SET serveroutput ON
- edit
- edit file name
- DECLARE
- a int;
- b int;
- c int;
- BEGIN
- a:=&a;
- b:=&b;
- c:=a+b;
- DBMS_OUTPUT.put_line('Sum of a and b is '||c);
- END;
- /
- output:
- SQL> @add
- Enter VALUE FOR a: 12
- old 6: a:=&a;
- NEW 6: a:=12;
- Enter VALUE FOR b: 58
- old 7: b:=&b;
- NEW 7: b:=58;
- SUM OF a AND b IS 70
- PL/SQL PROCEDURE successfully completed.
- 2. Area OF Circle
- DECLARE
- r NUMBER;
- pi NUMBER := 3.14;
- area NUMBER;
- BEGIN
- r:=&r;
- area := pi * r * r;
- DBMS_OUTPUT.put_line('Area of a circle: '||area);
- END;
- /
- OUTPUT:
- SQL> edit area
- SQL> @area
- Enter VALUE FOR r: 5
- old 6: r:=&r;
- NEW 6: r:=5;
- Area OF a circle: 78.5
- PL/SQL PROCEDURE successfully completed.
- 3. Biggest OF three numbers
- DECLARE
- a int;
- b int;
- c int;
- BEGIN
- a:=&a;
- b:=&b;
- c:=&c;
- IF(a>b AND a>c) THEN
- DBMS_OUTPUT.put_line('a is greatest');
- ELSIF(b>a AND b>c) THEN
- DBMS_OUTPUT.put_line('b is greatest');
- ELSIF(c>b) THEN
- DBMS_OUTPUT.put_line('c is greatest');
- ELSE
- DBMS_OUTPUT.put_line('a=b=c');
- END IF;
- END;
- /
- OUTPUT:
- SQL> SET serveroutput ON
- SQL> edit
- SP2-0107: Nothing TO save.
- SQL> edit tgreat
- SQL> @tgreat
- Enter VALUE FOR a: 12
- old 6: a:=&a;
- NEW 6: a:=12;
- Enter VALUE FOR b: 52
- old 7: b:=&b;
- NEW 7: b:=52;
- Enter VALUE FOR c: 32
- old 8: c:=&c;
- NEW 8: c:=32;
- b IS GREATEST
- PL/SQL PROCEDURE successfully completed.
- 4. Printing 1 TO n NATURAL numbers using
- (i) simple LOOP
- (ii) WHILE LOOP
- (iii) FOR LOOP
- DECLARE
- a int;
- b int:=1;
- i int;
- c int :=1;
- BEGIN
- a:=&a;
- DBMS_OUTPUT.put_line('Printing using simple loop');
- LOOP
- EXIT WHEN b>a;
- DBMS_OUTPUT.put_line(b);
- b:=b+1;
- END LOOP;
- DBMS_OUTPUT.put_line('Printing using while loop');
- WHILE (c<=a) LOOP
- DBMS_OUTPUT.put_line(c);
- c:=c+1;
- END LOOP;
- DBMS_OUTPUT.put_line('Printing using for loop');
- FOR b IN 1..a LOOP
- DBMS_OUTPUT.put_line(b);
- END LOOP;
- END;
- /
- OUTPUT:
- SQL> edit LOOP
- SQL> @LOOP
- Enter VALUE FOR a: 5
- old 7: a:=&a;
- NEW 7: a:=5;
- Printing using simple LOOP
- 1
- 2
- 3
- 4
- 5
- Printing using WHILE LOOP
- 1
- 2
- 3
- 4
- 5
- Printing using FOR LOOP
- 1
- 2
- 3
- 4
- 5
- PL/SQL PROCEDURE successfully completed.
- 5. Printing 1 TO n NATURAL numbers IN REVERSE using
- (i) simple LOOP
- (ii) WHILE LOOP
- (iii) FOR LOOP
- DECLARE
- a int;
- b int;
- i int;
- c int;
- BEGIN
- a:=&a;
- DBMS_OUTPUT.put_line('Printing using simple loop');
- LOOP
- EXIT WHEN a<1;
- DBMS_OUTPUT.put_line(a);
- a:=a-1;
- END LOOP;
- DBMS_OUTPUT.put_line('Printing using while loop');
- c:=&c;
- WHILE (c>0) LOOP
- DBMS_OUTPUT.put_line(c);
- c:=c-1;
- END LOOP;
- DBMS_OUTPUT.put_line('Printing using for loop');
- i:=&i;
- FOR b IN REVERSE 1..i LOOP
- DBMS_OUTPUT.put_line(b);
- END LOOP;
- END;
- /
- OUTPUT:
- SQL> edit rloop
- SQL> @rloop
- Enter VALUE FOR a: 5
- old 7: a:=&a;
- NEW 7: a:=5;
- Enter VALUE FOR c: 6
- old 15: c:=&c;
- NEW 15: c:=6;
- Enter VALUE FOR i: 8
- old 21: i:=&i;
- NEW 21: i:=8;
- Printing using simple LOOP
- 5
- 4
- 3
- 2
- 1
- Printing using WHILE LOOP
- 6
- 5
- 4
- 3
- 2
- 1
- Printing using FOR LOOP
- 8
- 7
- 6
- 5
- 4
- 3
- 2
- 1
- PL/SQL PROCEDURE successfully completed.
- 6. Printing Factorial OF a NUMBER
- DECLARE
- a int;
- f int :=1;
- BEGIN
- a:=&a;
- WHILE a>0 LOOP
- f:=f*a;
- a:=a-1;
- END LOOP;
- DBMS_OUTPUT.put_line(f);
- END;
- /
- OUTPUT:
- SQL> edit fac
- SQL> @fac
- Enter VALUE FOR a: 5
- old 5: a:=&a;
- NEW 5: a:=5;
- 120
- PL/SQL PROCEDURE successfully completed.
- 7. Printing Fibonacci series
- DECLARE
- a int:=0;
- b int:=1;
- n int;
- t int;
- i int;
- BEGIN
- n:=&n;
- DBMS_OUTPUT.put_line(a);
- DBMS_OUTPUT.put_line(b);
- FOR i IN 2..n LOOP
- t:=a+b;
- a:=b;
- b:=t;
- DBMS_OUTPUT.put_line(t);
- END LOOP;
- END;
- /
- OUTPUT:
- SQL> edit fib
- SQL> @fib
- Enter VALUE FOR n: 9
- old 8: n:=&n;
- NEW 8: n:=9;
- 0
- 1
- 1
- 2
- 3
- 5
- 8
- 13
- 21
- 34
- PL/SQL PROCEDURE successfully completed.
- 8.Printing SUM OF Digits (Eg: I/P 425 O/P 4+2+5=11)
- DECLARE
- a int;
- b int;
- s int;
- BEGIN
- a:=&a;
- s:=0;
- WHILE a > 0 LOOP
- b:=MOD(a,10);
- s:=s+b;
- a:=FLOOR(a/10);
- END LOOP;
- DBMS_OUTPUT.put_line('The sum of digits is: '||s);
- END;
- /
- OUTPUT:
- SQL> edit sdigit
- SQL> @sdigit
- Enter VALUE FOR a: 234
- old 6: a:=&a;
- NEW 6: a:=234;
- The SUM OF digits IS: 9
- PL/SQL PROCEDURE successfully completed.
- 9. Printing Palindrome.
- DECLARE
- a VARCHAR2(20):='naman';
- b VARCHAR2(20);
- c VARCHAR2(20);
- BEGIN
- FOR i IN REVERSE 1..LENGTH(a) LOOP
- b:=SUBSTR(a,i,1);
- c:=c||''||b;
- END LOOP;
- IF c = a
- THEN
- DBMS_OUTPUT.put_line(c||' is palndrome');
- ELSE
- DBMS_OUTPUT.put_line(c||' is not palndrome');
- END IF;
- END;
- /
- OUTPUT:
- SQL> edit plan
- SQL> @plan
- naman IS palndrome
- PL/SQL PROCEDURE successfully completed.
- 10. Adding only the odd numbers FROM USER input using LOOP. (Eg: I/P 4,8,7,2,3,9,6 O/P 7+3+9=19)
- DECLARE
- input_string VARCHAR2(100) := '4,5,6,7';
- total_odd_sum NUMBER := 0;
- num VARCHAR2(10);
- BEGIN
- LOOP
- num := SUBSTR(input_string, 1, INSTR(input_string, ',') - 1);
- input_string := SUBSTR(input_string, INSTR(input_string, ',') + 1);
- IF MOD(TO_NUMBER(num), 2) = 1 THEN
- total_odd_sum := total_odd_sum + TO_NUMBER(num);
- END IF;
- EXIT WHEN INSTR(input_string, ',') = 0;
- END LOOP;
- IF MOD(TO_NUMBER(input_string), 2) = 1 THEN
- total_odd_sum := total_odd_sum + TO_NUMBER(input_string);
- END IF;
- DBMS_OUTPUT.PUT_LINE('Sum of odd numbers: ' || total_odd_sum);
- END;
- /
- OUTPUT:
- SQL> edit osum
- SQL> @osum
- SUM OF odd numbers: 12
- PL/SQL PROCEDURE successfully completed.
- DBMS Lab 9.txt
- Displaying DBMS Lab 9.txt.
- Lab 9
- Gayathry S Warrier Computer Science (Yeshwanthpur)
- •
- Oct 7 (Edited Oct 7)
- 100 points
- Due Oct 7
- Dear students
- Complete the following list OF simple programs IN PL/SQL
- 1. Addition OF two numbers
- 2. Area OF Circle
- 3. Biggest OF three numbers
- 4. Printing 1 TO n NATURAL numbers using
- (i) simple LOOP
- (ii) WHILE LOOP
- (iii) FOR LOOP
- 5. Printing 1 TO n NATURAL numbers IN REVERSE using
- (i) simple LOOP
- (ii) WHILE LOOP
- (iii) FOR LOOP
- 6. Printing Factorial OF a NUMBER
- 7. Printing Fibonacci series
- 8.Printing SUM OF Digits (Eg: I/P 425 O/P 4+2+5=11)
- 9. Printing Palindrome.
- 10. Adding only the odd numbers FROM USER input using LOOP. (Eg: I/P 4,8,7,2,3,9,6 O/P 7+3+9=19)
- Class comments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement