Skip to main content

Oracle Cursor


/*Fetch Every Employee Details With the help of cursor*/
/*DBMS:SCOTT/TIGER:ORACLE 9i*/


SET SERVEROUTPUT ON
DECLARE
  VEMPNO EMP.EMPNO%TYPE;
  VENAME EMP.ENAME%TYPE;
  VSAL EMP.SAL%TYPE;

  CURSOR CR_EMP
  IS
  SELECT EMPNO,ENAME,SAL FROM EMP;

BEGIN
  OPEN CR_EMP;
  LOOP
    FETCH CR_EMP INTO VEMPNO,VENAME,VSAL;
  EXIT WHEN CR_EMP%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('EMPLOYEE DETAILS'); 
    DBMS_OUTPUT.PUT_LINE('----------------');
    DBMS_OUTPUT.PUT_LINE('ID: '||VEMPNO);
    DBMS_OUTPUT.PUT_LINE('NAME: '||VENAME);
    DBMS_OUTPUT.PUT_LINE('BASIC: '||VSAL);
    DBMS_OUTPUT.PUT_LINE('----------------');
  END LOOP;
  CLOSE CR_EMP;
END;

OR

/*Fetch Every Employee Details With the help of cursor*/
/*DBMS:SCOTT/TIGER:ORACLE 9i*/
/*WHILE LOOP*/


SET SERVEROUTPUT ON
DECLARE
  VEMPNO EMP.EMPNO%TYPE;
  VENAME EMP.ENAME%TYPE;
  VSAL EMP.SAL%TYPE;

  CURSOR CR_EMP
  IS
  SELECT EMPNO,ENAME,SAL FROM EMP;

BEGIN
  OPEN CR_EMP;
    FETCH CR_EMP INTO VEMPNO,VENAME,VSAL;
    WHILE CR_EMP%FOUND
    LOOP
      EXIT WHEN CR_EMP%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('EMPLOYEE DETAILS'); 
      DBMS_OUTPUT.PUT_LINE('----------------');
      DBMS_OUTPUT.PUT_LINE('ID: '||VEMPNO);
      DBMS_OUTPUT.PUT_LINE('NAME: '||VENAME);
      DBMS_OUTPUT.PUT_LINE('BASIC: '||VSAL);
      DBMS_OUTPUT.PUT_LINE('-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-');
    END LOOP;
END;



/*CALCULATE SUM OF TOTAL OF SALARY WHOSE INCOME LESS THAN 1000 USING CURSOR
/*ORACLE 9i 
/* PREDEFINED DATABASE := SCOTT/TIGER*/

SET SERVEROUTPUT ON
DECLARE
  VSUM_TL NUMBER:=0;
  VSAL NUMBER:=0;
  VENAME EMP.ENAME%TYPE;
  CURSOR T1 IS SELECT ENAME,SAL FROM EMP ORDER BY SAL;
BEGIN
  OPEN T1;
  LOOP
    FETCH T1 INTO VENAME,VSAL;
    EXIT WHEN NOT T1%FOUND;
    IF VSAL<=1500 THEN
      DBMS_OUTPUT.PUT_LINE(VENAME || ' : ' || VSAL);
      VSUM_TL := VSUM_TL + VSAL;
    END IF;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('----------------');
  DBMS_OUTPUT.PUT_LINE('         ' || VSUM_TL);
  CLOSE T1;
END;

Comments

Popular posts from this blog

Background Process in ORACLE Database

Background Process in ORACLE Database Database Writer Process (DBWn) Log Writer Process (LGWR) Checkpoint Process (CKPT) System Monitor Process (SMON) Process Monitor Process (PMON) Recoverer Process (RECO) Job Queue Processes Archiver Processes (ARCn) Queue Monitor Processes (QMNn) Database Writer Process (DBWn) The database writer process (DBWn) writes the contents of buffers to datafiles. The DBWn processes are responsible for writing modified (dirty) buffers in the database buffer cache to disk. Although one database writer process (DBW0) is adequate for most systems, you can configure additional processes (DBW1 through DBW9 and DBWa through DBWj) to improve write performance if your system modifies data heavily. These additional DBWn processes are not useful on uniprocessor systems. When a buffer in the database buffer cache is modified, it is marked dirty. A cold buffer is a buffer that has not been recently used according to the least recently used (LRU) algorith...

Using GREP in UNIX

How To Use grep Command In Linux / UNIX by  VIVEK GITE  on  AUGUST 2, 2007  ·  147 COMMENTS H ow do I use grep command in Linux? grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p grep command syntax grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something' Use grep to search file Search /etc/passwd for boo user: $ grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: $ grep -i "boo" /etc/passwd Use grep recursively You can search recursively i.e. read all files under each ...

A Hit Count Using Java Servlet's Session Tracking

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionTracker extends HttpServlet {         public void doGet(HttpServletRequest req,HttpServletResponse res)         throws ServletException,IOException         {                res.setContentType("text/html");                PrintWriter out=res.getWriter();                HttpSession session=req.getSession(true);                Integer count=(Integer)session.getValue("tracker.count");                if(count==null)       ...