Tuesday, September 27, 2011

A Day Out with under-privileged children


Recently we had a day out with under priviledge children from a Shishu Mandir School of Bangalore,
where most of the children are either orphan or has a single parent living and belongs to extremely poor families.
Me along with my office friends took all the kids of Shish Mandir (around 180 students,age 4-15 Yrs) with their teachers to Bannerghatta Wildlife Nature Camp.
It was a memorable moment to see such enthusiastic and loving kids with lots of love for fellow kids.








We Started from the School around 10:00 AM with lots of delay (curtsy to Bangalore traffic and our mismanagement)and reached the Bennarghatta 12:30 AM,on the way kids were served juice etc by the volunteers.Kids had lots of games on the way in the bus,their energy level was noticeable.
Once we reached there,started with the serving the food to the children and followed with games...



It was a full fun playing with those kids,i was not able to match their energy level :(
then we headed towards the safari ride to national park,kids were very excited to see loin n tigers.


After Safari,kids had a snacks n juices and proceeded with visiting the Zoo though it was already 5:00 PM when we were suppose to head back to school but again credit goes to our mis-management where we tried giving everything to kids without considering their needs that they should reach their homes on time.
Anyways at 6:30 PM we had a group photo with all the volunteers and kids


Then we had a nice session on Snakes By "Dr. Sreekumar Chirukandoth" - Tamil Nadu Veterinary and Animal Sciences university -Associate Professor.
Kids really enjoyed this session,you can make out from their faces how curious they were :)



By the time we finished all this ,it was 7:30 PM and we were far away from school and kids were about to loose their energy.
then we took 30 mins to arrange all the kids and send them to the buses and distributed snacks packets to all the kids.
Eventually started 8:00 PM from national park and on the ways kids started sleeping and then we released we don't have water bottles in the bus,Kids started crying (again credit goes to our mismanagement), somehow we managed to buy water bottles on the way to suffice their need.
Kids reached their school around 11:00 PM in the night by the time they were totally collapsed.
Point here is that we should be learn from our mistakes and try to avoid such mistakes in future because finally we did this to make kids happy not to suffice our ego that we really did something great doesn't matter how much kids has suffered because of our mismanagement.some points which we should have taken care were :
1. We should have planned the event timings by considering the Bangalore traffic.
2. We should have deployed necessary items in the buses before start. (e.g. water bottles, disposable glasses, some biscuits , tissue papers and most important: a first-aid box).
3. While traveling back to school, at least one volunteer  should have accompanied the kids and school staff till the school.(It was around 10:45 PM when they reached back school).
4. When we came to know that we were running out of time, we should have skipped some planned activities to finish our program within a reasonable time frame.



Hope this will be learning from others who are planning to conduct such events in near future.

Monday, September 26, 2011

Ref Cursors

Just wondering from sometimes that why the hell we have one more cursor type called as ref cursor when we already have normal cursors to solve our purpose of storing the SQL result set in to the memory for accessing them later.
before starting the difference between ref cursor and a normal cursor,let me first make it clear what a Ref cursor is ,what are their types and some of their usage.


What is Ref Cursor ?



Ref Cursor is basically a PL/SQL Datatype. This is used to store 
result of a query. It is a "pointer" data types that allows you to quickly reference any cursor result with data values kept in RAM.
A variable created based on such a data type is generally called a cursor variable.A cursor variable can be associated with different queries at run-time.
The primary advantage of using cursor variables is their capability to pass result sets between sub programs (like stored procedures, functions, packages etc.)
declare
  -- defines a new data type called "rf_cursor," which is of the type REF CURSOR   type rf_cursor is REF CURSOR; 
  -- We declare a cursor variable named "c_emp" based on the type "rf_cursor"    
  c_emp rf_cursor;
  en emp.ename%type; begin
  -- Every cursor variable must be opened with an associated SELECT statement as follows:   open c_emp for select ename from emp;   loop       fetch c_emp into en;       exit when c_emp%notfound;       dbms_output.put_line(en);   end loop;
  --closed the cursor    close c_emp; end;
Types of Ref Cursors
Ref cursor type can be strong(restrictive) or weak (non restrictive).
A strong REF CURSOR type definition specifies a return type, but a weak definition does not.
DECLARE 
  TYPE strong_cursor IS REF CURSOR RETURN emp%ROWTYPE -- strong
  TYPE weak_cursor IS REF CURSOR; --weak 
NOTE : Declaring a cursor variable creats a pointer, not an item.
Cursor variables can not be saved in the database.

Cursors - Vs - Ref Cursors

1. A "normal" plsql cursor is static in defintion. Ref cursors may be dynamically opened or opened based on logic.

Declare
  type rc is ref cursor;
 
  cursor c is select * from dual;

  l_cursor rc;
begin
  if ( to_char(sysdate,'dd') = 30 ) then
    open l_cursor for 'select * from emp';
  elsif ( to_char(sysdate,'dd') = 29 ) then
    open l_cursor for select * from dept;
  else
    open l_cursor for select * from dual;
  end if;
  open c;
end;
/
Given that block of code -- you see perhaps the most "salient" difference -- no  matter how many times you run that block -- cursor C will always be select *  from dual. The ref cursor can be anything.
2. Another difference is a ref cursor can be returned to a client. a plsql "cursor  cursor" cannot be returned to a client. 
Also, a ref cursor can be passed from subroutine to subroutine -- a cursor cannot be.
Some examples :
CREATE OR REPLACE PACKAGE CURSPKG AS 
    TYPE T_CURSOR IS REF CURSOR; 
    PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, 
                               IO_CURSOR IN OUT T_CURSOR); 
    PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR, 
                                DEPTCURSOR OUT T_CURSOR);
END CURSPKG;
/ 
CREATE OR REPLACE PACKAGE BODY CURSPKG AS
    PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER,
                               IO_CURSOR IN OUT T_CURSOR)
    IS 
        V_CURSOR T_CURSOR; 
    BEGIN 
        IF N_EMPNO <> 0 
        THEN
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO 
                  AND EMP.EMPNO = N_EMPNO;

        ELSE 
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO;

        END IF;
        IO_CURSOR := V_CURSOR; 
    END OPEN_ONE_CURSOR; 

    PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR,
                                DEPTCURSOR OUT T_CURSOR)
    IS 
        V_CURSOR1 T_CURSOR; 
        V_CURSOR2 T_CURSOR; 
    BEGIN 
        OPEN V_CURSOR1 FOR SELECT * FROM EMP;
        OPEN V_CURSOR2 FOR SELECT * FROM DEPT;
        EMPCURSOR  := V_CURSOR1; 
        DEPTCURSOR := V_CURSOR2; 
    END OPEN_TWO_CURSORS; 
END CURSPKG;
/
Another Example of Ref Cursor getting called in the package :
CREATE PACKAGE my_cursor AS

   TYPE MyCursor IS REF CURSOR;
   ...
END my_cursor;


CREATE PROCEDURE GetCarter ( proc_cv IN OUT my_cursor.MyCursor,
                             emp_name VARCHAR2(50) )
   ...

Then the client-side application code would start like

DECLARE
    local_cv        my_cursor.MyCursor;
    carter_record   carter%ROWTYPE
BEGIN
   GetCarter(local_cv,:employee)    -- employee is a host variable
   FETCH local_cv INTO carter_record;
   ...
END
/