Thursday, February 14, 2008

Cursor in MYSQL

Cursor in MYSQL


Cursor :

 
1 You need to declare a cursor for t1 table to go through its each and every record.
2 Open the cursor I have no idea about your tables in detail, if you have only one record in t2 table on base of t1 table, you can write a select statement to get that record or to check whether it is exists or not in t2 table but if you have multiple record then you need to write another cursor for t2 table to fetch each record and to get each an every record, and check whether record exist or not,if exist take another record from t1 else insert the t1 record into t2.
4 close cursor.
To use cursor in procedure there are four steps, declare, open,fetch and close.

example :

  CREATE PROCEDURE procedure1(empno INTEGER)               /* name */
           
 BEGIN                                      /* start of block */
 
  DECLARE cur CURSOR FOR
   // Use any one query from below
    SELECT empid FROM employee WHERE empid=empno;
     SELECT empid FROM employee WHERE ;
  open curl;
     emp_loop:LOOP
       FETCH curl INTO employeeId;
     END LOOP emp_loop;
  CLOSE cur;
 END                                       /* end of block */


In PHP File:
 
$link = mysqli_connect('localhost','root', '', 'DATASERVICE_SAMPLE');

 $result = mysqli_query($link,"CALL procedure1('1002')");

No comments:

Post a Comment