cursor types? explain with example programs?



cursor types? explain with example programs?..

Answer / chandra shekhar

Cursor Types
- Implicit Cursor
- Explicit Cursor (Without parameters, With Parameters)


############################################
##### Implicit Cursor #####################
############################################

-- assuming there is a table emp in the schema
DECLARE
VENAME EMP.ENAME%TYPE ;
BEGIN
SELECT ENAME INTO VENAME FROM EMP WHERE EMPNO = &EMPLOYEENUMBER ;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME IS ' || VENAME);
END IF ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO EMPLOYEE WITH GIVE EMPLOYEE NUMBER ') ;
END ;
/

############################################
##### Explicit Cursor #####################
############################################

-- First method
===============

DECLARE
CURSOR C1 IS SELECT EMPNO, ENAME FROM EMP ;
BEGIN
FOR I IN C1
LOOP
DBMS_OUTPUT.PUT(I.EMPNO);
DBMS_OUTPUT.PUT_LINE(' -- ' || I.ENAME);
END LOOP ;
END ;
/

-- Second method
================

DECLARE
CURSOR C1 IS SELECT EMPNO, ENAME FROM EMP ;
E C1%ROWTYPE ;
BEGIN
OPEN C1 ;
LOOP
FETCH C1 INTO E ;
EXIT WHEN C1%NOTFOUND ;
DBMS_OUTPUT.PUT(E.EMPNO);
DBMS_OUTPUT.PUT_LINE(' -- ' || E.ENAME);
END LOOP ;
CLOSE C1 ;
END ;
/

-- With Parameters
==================

DECLARE
CURSOR C1(n number) IS SELECT EMPNO, ENAME FROM EMP where sal < n;
E C1%ROWTYPE ;
BEGIN
OPEN C1(&salary) ;
loop
FETCH C1 INTO E ;
exit when c1%notfound ;
DBMS_OUTPUT.PUT(E.EMPNO);
DBMS_OUTPUT.PUT_LINE(' -- ' || E.ENAME);
end loop ;
CLOSE C1 ;
END ;
/

Is This Answer Correct ?    4 Yes 1 No

Post New Answer

More SQL PLSQL Interview Questions

What is union and union all keyword in sql?

0 Answers  


write sub query for eliminating duplicate rows using analytical function?

3 Answers   Metric Stream,


what are the differences between procedure-oriented languages and object-oriented languages? : Sql dba

0 Answers  


How do I find duplicates in sql?

0 Answers  


Which certification is best for sql?

0 Answers  






Where is all the data on the internet stored?

0 Answers  


Can we debug stored procedure?

0 Answers  


3. Select sum(a) sum_1,max(a) max_1 ,count(a) count_1 from ( ( select 1 a from dual union all Select to_number(‘2011’) a from dual union all select 1 a from dual) union select 2 b from dual);

1 Answers   Fintellix,


what is a foreign key ? : Sql dba

0 Answers  


What is an invalid partition table?

0 Answers  


How many joins in sql?

0 Answers  


Explain how you can copy a file to file content and file to pl/sql table in advance pl/sql?

0 Answers  


Categories