May 2, 2009

CURSORS



IMPLICIT CURSOR

DELETE

 begin
 delete from worker where deptno=20;
 delete from worker where deptno=90;
 delete from worker where deptno=10;  // this is taken as SQL in the next line
 if(sql%found) then
 dbms_output.put_line('The deleted rows are'||SQL%ROWCOUNT);
 else
 dbms_output.put_line('There is no such department');
 end if;
 end;
/

output:

The deleted rows are 3  ( there are 3 rows that has deptno as 10)

INSERT

 begin 
 insert into worker(empno,ename,sal) values(3489,'joe',2300);
 if(sql%found) then
 dbms_output.put_line('A new row has been inserted');
 end if;
 if(sql%notfound) then
 dbms_output.put_line('Prob during insertion');
 end if;
 end;
 /

A new row has been inserted

PL/SQL procedure successfully completed

Similarly we can use update…

Using loops

SQL>  begin
  2   for indx in 1..3
  3   loop
  4   delete from worker where sal>1000;
  5    if(sql%found) then
  6    dbms_output.put_line('The deleted rows are'||SQL%ROWCOUNT);
  7    else
  8  dbms_output.put_line('Already deleted');
  9  end if;
 10  end loop;
 11  end;
 12  /



Output:

The deleted rows are20
Already deleted
Already deleted

PL/SQL procedure successfully completed.

SQL> set serveroutput on;
SQL> begin 
  2  insert into worker(empno,ename,sal) values(3489,'joe',2300);
  3  insert into worker(empno,ename,sal) values(1234,'vithya',3400);
  4  insert into worker(empno,ename,sal) values(4689,'kar',7890); //this is taken as SQL in the next line
       if(sql%found) then
  5   dbms_output.put_line(sql%rowcount);
  6  else
  7   dbms_output.put_line('Prob during insertion');
  8   end if;
  9  end;
 10  /

1

PL/SQL procedure successfully completed.



SQL>  declare 
  2   myemp worker%rowtype;
  3   Begin
  4   select *into myemp from worker where ename='smith';
  5    if(sql%found) then
  6   dbms_output.put_line(sql%rowcount);
  7   elsif(SQL%notfound)
  8   then  
  9   dbms_output.put_line('error');     /there can be if elsif with out else part
 10  end if;
 11  end;
 12  /

 /exception thrown... if no rows selected.... so ‘error’ not printed as in 9th statement /

 declare   
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4


SQL>  declare 
  2   myemp worker%rowtype;
  3   Begin
  4   select *into myemp from worker where ename='SMITH';  /use into clause  for select statement here otherwise it will show error 
  5    if(sql%found) then
  6   dbms_output.put_line(sql%rowcount);
  7   end if;
  8   end;
  9  /

Output:

1

PL/SQL procedure successfully completed.


EXPLICIT CURSOR

SQL>  declare
  2   cursor c is select *from worker where deptno=10;
  3   c1 c%rowtype;
  4   begin
  5   open c;
  6   loop
  7   fetch c into c1;
  8   if(c%found) then
  9   dbms_output.put_line(c%rowcount);
 10   else
 11   exit;
 12   end if;
 13   end loop;
 14   close c;
 15   end;
 16  /
1
2
3

SQL>  declare
  2   cursor c is select *from worker where deptno=10;
  3   c1 c%rowtype;
  4   begin
  5   open c;
  6   loop
  7   fetch c into c1;
  8   if(c%found) then
  9   dbms_output.put_line(c%rowcount);
 10  dbms_output.put_line(c1.ename);
 11   else
 12   exit;
 13   end if;
 14   end loop;
 15   close c;
 16   end;
 17  /


1
CLARK
2
KING
3
MILLER

PL/SQL procedure successfully completed.



 1   declare
 2   cursor c is select *from worker where deptno=10;
 3   c1 c%rowtype;
 4   begin
 5   open c;
 6   dbms_output.put_line(c%rowcount);
 7   end;

Output:

0


SQL>  declare
  2   cursor c is select *from worker where deptno=10;
  3   c1 c%rowtype;
  4   begin
  5   open c;
  6   loop
  7   fetch c into c1;
  8   if(c%found) then
  9   dbms_output.put_line(c%rowcount);dbms_output.put_line(c.ename);
 10   else
 11   exit;
 12   end if;
 13   end loop;
 14   close c;
 15   end;
 16  /
 dbms_output.put_line(c%rowcount);dbms_output.put_line(c.ename);
                                                         *
ERROR at line 9:
ORA-06550: line 9, column 58:
PLS-00225: subprogram or cursor 'C' reference is out of scope
ORA-06550: line 9, column 35:
PL/SQL: Statement ignored


C  is  the cursor pointing to the each fields in a row of a table and C1 is like temporary storage here C is fetched to C1 so C1.ename works not c.ename …


USING CURSOR TO FETCH EACH ROWS TO A RECORD

SQL>  declare
  2   type rec is record(n worker.empno%type,name worker.ename%type,sal worker.sal%type);
  3    r rec;
  4  cursor c is select empno,ename,sal from worker where ename='SMITH';
  5   begin
  6   open c;
  7  fetch c into r;
  8  if(c%found) then
  9  dbms_output.put_line(c%rowcount);
 10  dbms_output.put_line(r.n||' '||r.name||' '||r.sal);
 11  end if;
 12  end;
 13  /


1
7369 SMITH 3500

PL/SQL procedure successfully completed.

  1   declare
  2   type rec is record(name emp.ename%type,salary emp.sal%type);
  3   cursor c is select ename,sal from emp where deptno=10;
  4   r rec;
  5   begin
  6   open c;
  7   loop
  8   fetch c into r;
  9   if(c%found) then
 10   dbms_output.put_line(c%rowcount);
 11   dbms_output.put_line(r.name ||'  '||r.salary);
 12   else
 13   exit;
 14   end if;
 15   end loop;
 16   close c;
 17*  end;


SQL> /
1
CLARK  2450
2
KING  5000
3
karthik  1300


USING CURSOR FOR LOOP


  1   DECLARE
  2   CURSOR C IS SELECT ENAME,SAL,DEPTNO FROM EMP;
  3   REC  C%ROWTYPE; // R is of rowtype Cursor C
  4   BEGIN
  5   FOR REC IN C
  6   LOOP
  7   IF(REC.DEPTNO=10) THEN
  8   DBMS_OUTPUT.PUT_LINE(REC.ENAME ||'   '||REC.SAL);
  9   END IF;
 10   END LOOP;
 11*  END;
SQL> /
CLARK   2450
KING   5000
karthik   1300

open,fetch,close everything did implicitly here...


  1   DECLARE
  2   CURSOR C IS SELECT ENAME,SAL,DEPTNO FROM EMP;
  3   BEGIN
  4   FOR REC IN C
  5   LOOP
  6   IF(REC.DEPTNO=10) THEN
  7   DBMS_OUTPUT.PUT_LINE(REC.ENAME ||'   '||REC.SAL);
  8   END IF;
  9   END LOOP;
 10*  END;
SQL> /
CLARK   2450
KING   5000
karthik   1300

in the above case statement 3   REC  EMP%ROWTYPE is missing but still it works fine as it invokes REC implicitly from previous execution


Further simplification...

  1   BEGIN
  2   FOR REC IN (SELECT ENAME,SAL,DEPTNO FROM EMP)
  3   LOOP
  4   IF(REC.DEPTNO=10) THEN
  5   DBMS_OUTPUT.PUT_LINE(REC.ENAME ||'   '||REC.SAL);
  6   END IF;
  7   END LOOP;
  8*  END;
SQL> /
CLARK   2450
KING   5000
karthik   1300


CURSOR WITH PARAMETERS

  1   DECLARE
  2   CURSOR C(INPUT NUMBER) IS SELECT COUNT(*) CNT FROM EMP WHERE DEPTNO=INPUT;
  3   R  C%ROWTYPE; 
  4   BEGIN
  5   OPEN C(10);
  6   FETCH C INTO R;
  7   DBMS_OUTPUT.PUT_LINE('No of emp in deptno 10 is  ' ||R.CNT);
  8   CLOSE C;
  9   OPEN C(20);
 10   FETCH C  INTO R;
 11   DBMS_OUTPUT.PUT_LINE('No of emp in deptno 20 is  ' ||R.CNT);
 12   CLOSE C;
 13*  END;

SQL> /
No of emp in deptno 10 is  3
No of emp in deptno 20 is  5


LOCKING


1.ROW LEVEL
2.TABLE LEVEL


ROW LEVEL

FOR UPDATE [OF COLUMN] [NOWAIT]

refer pg no.49 in book


SESSION 1:
SELECT *FROM EMP WHERE DEPTNO=10 FOR UPDATE NOWAIT;
SESSION 2: it's not possi for us to update emp details of deptno=10 till session 1 releases lock


TABLE LEVEL

1.SHARE - This mode is for read only on entire table.we can not make changes to table.
2.SHARE UPDATE - This mode is used to lock the selected rows for update.This mode acquires lock on selected rows only,not entire table.Other user can have lock on other rows on the same table but not on the rows you have locked.
3.EXCLUSIVE  - High priority mode if this lock is enabled then one cant do nothing on the table.


SQL> LOCK TABLE STUDENT IN SHARE MODE;

Table(s) Locked.


SQL> LOCK TABLE STUDENT IN SHARE UPDATE MODE;

Table(s) Locked.


SQL> LOCK TABLE STUDENT IN EXCLUSIVE MODE;




WHERE CURRENT OF

should be used only with cursors and locked with for update option



is the latest craze. for more info explore the link above...

you can even look at www.annuniv.edu for more info



Apr 23, 2009

Packages in pl/sql



A simple example

package specification

SQL> create or replace package calculator
  2  as 
  3   procedure addition(n1 number,n2 number,add OUT number);
  4   procedure mul(no1 number,no2 number,product OUT number);
  5   end calculator;
  6  /

Package created.

package body

SQL> create or replace package body calculator
  2  is
  3  Procedure addition(n1 number,n2 number,add OUT number)
  4  as
  5  begin
  6  add := n1+n2;
  7  dbms_output.put_line(add);
  8  end addition;
  9  Procedure mul(no1 number,no2 number,product OUT number)
 10  as
 11  begin
 12  product := no1*no2;
 13  dbms_output.put_line(product);
 14  end mul;
 15  end calculator;
 16  /

Package body created.

SQL> var jose number;
SQL> exec calculator.addition(23,6,:jose);
29

PL/SQL procedure successfully completed.


Removing packages


Drop package package_name;

Drop package body package_name;



Bodiless package

SQL> CREATE OR REPLACE PACKAGE CONSTANTS
  2  IS
  3  PI NUMBER := 3.14;
  4  END CONSTANTS;
  5  /

SQL> EXEC DBMS_OUTPUT.PUT_LINE(CONSTANTS.PI);

3.14

To execute a package memeber


EXEC PACKAGE_NAME.function()[or procedure() or constant]




Method overloading possible inside package 

  1  create procedure B(a number)
  2  is
  3  begin
  4  dbms_output.put_line(a);
  5* end;
SQL> /

Procedure created.

SQL> create procedure B(a number,b number)
  2  is 
  3   begin
  4   dbms_output.put_line(a||b);
  5  end;
  6  /
create procedure B(a number,b number)
                 *
ERROR at line 1:
ORA-00955: name is already used by an existing object

But by using packages we can overload the above procedures...

Misc

SQL> create package pp
  2  as
  3  procedure pop(v varchar2);
  4  procedure pop(x varchar2);
  5  end;
  6  /

Package created.

To execute above use 

exec pp.pop(x=>’kar’);
exec.pp.pop(v=>’sel’):



One time only procedure

 It has no package body with in the package specification itself procedure definition is coded...Refer book

Advantages:


1.Security (Granting privileges)
2.Global access (Sharable objects)
3.Modularity (Grouping related objects)
4.Min disk space


points to remember:

The variables that are declared inside package specification are called public scope variables and those with in package body are called private.
Variables declared inside the package specification have NULL value if not initialized.
The public variables can be used or re initialized inside a package or in the exec statement.
We can invoke a function from a procedure inside the same package just by calling its name and appropriate parameter passing.
Package within another package not allowed.
Cursor variables cannot be declared inside package specification it can be used only inside pl-sql block.


Pragma restrict_references(funname,wnds,wnps,rnds,rnps)




Feb 20, 2009

SUN quiz

Here's your first look at a range of new and exciting benefits available only to Sun Developer Network members. Lots of free training being offered this quarter. Just another example of how your all access Backstage Pass from Sun is working for you.

SDN Quiz Sweepstakes — Test your NetBeans IDE Knowledge 
Take the SDN Quiz to see how much you know about NetBeans IDE. Answer the questions correctly for a chance to win one of 50 free service requests (single instance) through the Developer Expert Assistance Program (US $49) 
Take the quiz now! 

New Features in Project Kenai! 
We've been busy worker bees building additional features in Project Kenai. We're rolling out SCM support for a directory file system for your download area, and Atom feeds for your wikis. Use your SDN credentials to log in. Want to host an open source project at Kenai? Please send an invitation request e-mail to: kenai-admin@sun.com. Also look at auto insurance for teenagers

C ptrs

Ptr
Ptr2ptr
Void ptr
Fn ptr
Void ptr fn (not like void ptr)
Pointer to an array
Array of ptrs
 

 Int *p;

 There will be a memory space reserved for 2 bytes which can store address of integer type.

 &p represents address of the pointer variable. Mostly we won’t use this..

P = &somevariable

That Is P will gives the address of the variable it points to.

*p= some value , will make somevariable to assign some value.

 We can have **p ,***p,****p so on…

 Always p can hold some address of int type…

 Int in int *p implies the somwvariable value is of int type…

 Char in char *p implies somevariable value is of char type…

 Void in void *p implies the somevariable can be of any type.. it is known only at run time…

 We can also have pointer functions…

 Consider a function int p(int a) that returns ‘a’ value

Int (*ptf) (int) = &p; // the braces are mandatory…

 also we can call like ptf = p;

 We can invoke the function by 2 way..

 P(2);

(*ptf) (2);

 Both will return 2 as output.

 
these thing will work fine in laptop memory

sample program

# include

# include

 

 void func1(int (*a)[10][10])

{

printf("%d  ",a);

}

void func2(int a[][][])

{

printf("%d",a);

}

 

 

int main()

{

int a[10][10][10],b[10];

printf("%d   ",a);

func1(a);

func2(a);

 

getch();

return 0;

}

 




Feb 2, 2009

Discovering the best of Europe

Europe offers some of the world’s most exciting cities, romantic landscapes, outstanding museums, historic sights, culinary creations, and architectural wonders. You can sail past decaying palaces and sinking churches on Venice’s Grand Canal for the price of a bus ticket. You can drain creamy mugs of Guinness while clapping along to traditional Celtic music on a pub-crawl through Dublin. You can splurge on a 5-star meal in Paris, the Mecca of haute cuisine. Or you can wander through the Tower of London, ground

Zero for so much English history over the last 900 years. You may want to stare for hours at the famed scene of God Creating Adam on Michelangelo’s Sistine Chapel ceiling in Rome. Or sit atop Switzerland’s Schilthorn Mountain, surrounded by peaks covered with

snow and glacier-filled valleys, while eating breakfast in a revolving restaurant at 10,000 feet. Or enjoy a picnic lunch on the Greek island of SantorĂ­ni hundreds of feet above the Mediterranean amid the ruins of a Mycenaean city. Europe is yours to discover and experience. Dont ever forget to visit at extended stay hotels there...

Jan 12, 2009

Just for fun!!!

The local bar was so sure that its bartender was the strongest man around that they offered a standing $1,000.00 bet. The bartender would squeeze a lemon until all the juice ran into a glass, and hand the lemon to a patron. Anyone who could squeeze one more drop of juice out would win the money. Many people had tried over time (weightlifters, longshoremen, etc.) but nobody could do it.One day this scrawny little man came in, wearing thick glasses and a polyester suit, and said in a tiny, squeaky voice, "I'd like to try the bet."

After the laughter had died down, the bartender said OK, grabbed a lemon, and squeezed away. Then he handed the wrinkled remains of the rind to the little man. But the crowd's laughter turned to total silence as the man clenched his fist around the lemon and six drops fell into the glass.As the crowd cheered, the bartender paid the $1,000.00, and asked the little man, "what do you do for a living? Are you a lumberjack, a weightlifter, or what?"

The man replied, "I work for the las vegas hotel Income Tax Department."