Skip to main content

Inroduction to ORACLE Cursor

/*Fetch Every Emloyee 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 Emloyee 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

Sample : String Reverse In Java

import java.io.*; class test {     public static void main(String args[])     {         DataInputStream in=new DataInputStream(System.in);         try         {             String text;             System.out.println("\nEnter The Text");             text=in.readLine();             System.out.println("\nIn Reverse Order::\n");             for(int i=text.length()-1;i>=0;i--)             {                 System.out.print(text.charAt(i));             }          ...

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 ...