(I) Programs Using Cursors, Cursor Loops and Records
(I) Programs Using Cursors, Cursor Loops and Records
(I) Programs Using Cursors, Cursor Loops and Records
1. write a pl/sql program to get the name & price of some specific product(identified by Product_id)
Set serveroutput on; declare name product.description%type; amount product.price%type; Begin select description,price into name,amount from product where product_id=4; Dbms_output.put_line('price of'||name||'is'||amount); End; 2. write a pl/sql program to get the name & price of some specific product(identified by Product_id) using loops
Declare P product%rowtype; Cursor productcursor is select* from product; Begin Open productcursor; Loop Fetch productcursor into p; Exit when productcursor%notfound; Dbms_output.put_line(price of||p.description||is||p.price); End loop; Close productcursor; End; /
2
3. To write a Cursor to display List of Employees from Emp Table in PL/SQL block
INPUT DECLARE cursor c is select empno, ename, deptno, sal from emp ; i emp.empno%type; j emp.ename%type; k emp.deptno%type; l emp.sal%type; BEGIN open c; dbms_RESULT.put_line('Empno, name, deptno, salary of employees are:= '); loop fetch c into i, j, k, l; exit when c%notfound; dbms_RESULT.put_line(i||' '||j||' '||k||' '||l); end loop; close c; END;
4. To write a Cursor to find employee with given job and deptno. INPUT DECLARE cursor c1(j varchar2, dn number) is select empno, ename from emp where job=j and deptno=dn; row1 emp%rowtype; jb emp.job%type; d emp.deptno%type; BEGIN jb:='&jb'; d:=&d; open c1(jb,d);
3
fetch c1 into row1.empno,row1.ename; if c1%notfound then dbms_RESULT.put_line('Employee does not exist'); else dbms_RESULT.put_line('empno is:'||row1.empno||' ' ||'employee name is:'||row1.ename); end if; END;
PL/SQL Programming II
(i) Creating stored procedures, functions and packages Procedures
1. Write a procedure to retrieve the salary of a particular employee.
CREATE OR REPLACE PROCEDURE RET_SAL(EMPNUM NUMBER) IS VSAL EMP.SAL%TYPE; BEGIN SELECT SAL INTO VSAL FROM EMP WHERE EMPNO = EMPNUM; DBMS_OUTPUT.PUT_LINE('SALARY OF '|| EMPNUM || ' IS '|| VSAL); END; Output: SQL> exec ret_sal(7369); SALARY OF 7369 IS 800 PL/SQL procedure successfully completed. 2. Write a pl/sql block to calculate the factorial of a given number using stored procedure. CREATE OR REPLACE PROCEDURE FACTOR(N NUMBER) IS I NUMBER; FACT NUMBER := 1; BEGIN FOR I IN 1..N LOOP FACT := FACT * I; END LOOP; DBMS_OUTPUT.PUT_LINE('FACTORIAL OF '||N||' IS '||FACT); END;
4
Output: SQL> exec factor(5); FACTORIAL OF 5 IS 120 PL/SQL procedure successfully completed.
PROCEDURE DEFINITION:
CREATE or REPLACE PROCEDURE isprimepro(num in number,chec out number) IS temp NUMBER; BEGIN temp:=num; FOR itr IN 2..(num-1) LOOP IF(mod(num,itr)=0) THEN chec:=1; END IF; END LOOP; END; PL/SQL BLOCK TO CALL THE PROCEDURE:
DECLARE chec NUMBER; given_number NUMBER; BEGIN given_number:=&given_number; isprimepro(given_number,chec); IF chec=1 THEN dbms_output.put_line('number is not prime'); ELSE dbms_output.put_line('number is prime'); END IF; END;
5
Output: Enter value for given_number: 5 old 5: new 5: given_number:=&given_number; given_number:=5;
Functions
1. Write a Function to check the given number is prime or not. FUNCTION DEFINITION : CREATE or REPLACE FUNCTION isprime(num in number) RETURN number IS temp NUMBER; BEGIN temp:=num; FOR itr IN 2..(num-1) LOOP IF (mod(temp,itr)=0) THEN return 1; END IF; END LOOP; return 0; END; PL/SQL BLOCK TO CALL THE FUNCTION: DECLARE given_number NUMBER; prime_check NUMBER; BEGIN given_number:=&given_number; prime_check:=isprime(given_number); IF prime_check=0 THEN dbms_output.put_line('NUMBER IS PRIME'); ELSE dbms_output.put_line('NUMBER IS NOT PRIME'); END IF; END;
6
Output: Enter value for given_number: 10 old 5: new 5: given_number:=&given_number; given_number:=10;
2. Write a Function to calculate the area of a circle for a given value of radius. store the radius & corresponding area values in areas table
FUNCTION DEFINITION : create or replace function carea(rad in number) return number is pi constant number(4,2) :=3.14; area number(14,2); begin area :=pi*power(rad,2); insert into areas values(area,rad); return 1; end;
PL/SQL BLOCK TO CALL THE FUNCTION: declare radius number(3); chec number(2):=0; begin radius:=&radius; chec:=carea(radius); if chec=0 then dbms_output.put_line('unable to insert record'); else dbms_output.put_line('one record is inserted into areas table'); end if; end;
7
Output: Enter value for radius: 5 old 5: new 5: radius:=&radius; radius:=5;
one record is inserted into areas table PL/SQL procedure successfully completed.
set serverout on
Type created. SQL> create or replace 2 type body person as 3 4 5 6 7 8 9 10 member function get_last_name return varchar2 is begin return self.last_name; end; member function get_phone_number return varchar2 is begin return self.phone; end;
11 end; 12 /
8
SQL> declare 2 l_person person; 3 begin 4 l_person := person( 'C', 'B' ); 5 dbms_output.put_line( l_person.last_name ); 6 end; 7 / C PL/SQL procedure successfully completed.