views:

41

answers:

1

How To process a result in a stored procedure Suppose i have

sql = "SELECT fname, lname FROM students WHERE name ='Sam' ";

How will i fetch this result in variable and loop in a SP

like we do in PHP as:

$sql = "SELECT fname, lname FROM students WHERE name ='Sam' ";
$qSql = mysql_query($sql);
if ($qSql )
{
 $fArr = mysql_fetch_array($qSql);
 if ($fArr)
 {
  foreach ($fArr as $arrTmp) 
  {
   $arrResult['fName'] = $arrTmp['fname'];
   $arrResult['lName'] = $arrTmp['lname']; 
  }
 }
}

How we can do such type of looping in SP ?

How can we split string in SP and loop as we do in PHP ?

A: 

To loop through a result set you would normally use a cursor.

Can you describe why you are doing it? Where cursors are used there is often a more efficient way of doing things without.

Martin Smith
THX for ur help and question too.My First Query will get the main result ie the user details, then i want to loop it to fetch their intrest, hobbies, their friends name. Normally i used to below sequence to get desired result1. I will fetch the desired set of Users2. Then i wolud loop in PHP and get from single query each of the following:1. Their Intrest 2. Their Friends3. Their HobbiesThis was the actual scenario, How i used to do previuously. Previously, MySQL did not support SP and many joins would bringdown MySQL. So now i want all this process in a single procedure.But How ??
Also How to split string using SP and loop the string ?
How many records have you got? This definitely sounds like something best done with a JOIN. Maybe you're missing some needed indexes if it is too slow?
Martin Smith
Is there any array provision in SP or How can we simulate array in SP?