0

我有一个从 MSSQL 数据库返回多个结果集的存储过程。我遇到的问题是遍历每个结果集。似乎 Yii 没有推进到下一个结果集来遍历行。这是一个例子:

结果 1:

TOTAL
-----
999

结果 2:

ID |NAME
---|----
0  | Jon
1  | Bob
2  | Sarah
3  | Pete

这是我在 Yii 中进行这项工作的尝试。

//create the call to stored proc

$command=parent::$db->createCommand("exec sp_showUsers");    

//run the query      

$dataReader = $command->query();

//For the current object assign the row to a variable

while (($row = $dataReader->read()) !== false){

      //if the row has a total column

      if(isset($row['total'])){
           $total = $row['total'];
      }
 }

//Test if there is another result 

$usersExist = $dataReader->nextResult();

//$dataReader->next(); - REMOVED AS NOT NEEDED

if($usersExist){
     $userTable = array();
     $i = 0;
     while (($userRow = $dataReader->read())!== false){
            //add each row to a temporary array

            $userTable[$i] = $userRow['id'];
            $i++;
     }
 }

 ...

->next()即使已调用该方法,这似乎也不会遍历第二个结果集?任何帮助将不胜感激!谢谢。

Ps 存储过程确实有效,我可以使用普通 PHP 和sqlsrv_next_result()方法循环遍历结果。

4

2 回答 2

0

在代码中,为什么

$dataReader->nextResult()将返回一个CDbDataReader对象。

因此,您使用$dataReader->next();它只会移动第一个结果的指针。您必须移动nextResult()调用返回的指针。

我认为,

$dataReader=$dataReader->next();

会解决问题

于 2012-05-30T13:22:09.537 回答
0

我找到了答案。正是我们将参数绑定到我们的存储过程的方式弄乱了我们的结果。我们不需要 $dataReader->next(); 一点也不。所以上面的代码一切正常。我希望这可以帮助那些在 Yii 中使用 SQL Server 存储过程的人。:) 以防万一有人需要存储过程调用示例:

$command=parent::$db->createCommand("exec sp_showUsers :pageNumber, :pageSize, :sortOrder, :viewAll");

$command->bindParam(":pageSize", $pageSize, PDO::PARAM_INT); 
$command->bindParam(":pageNumber", $pageNumber, PDO::PARAM_INT); 
$command->bindParam(":sortOrder", $sortOrder, PDO::PARAM_STR); 
$command->bindParam(":viewAll", $viewAll, PDO::PARAM_BOOL); 

$dataReader = $command->query();
于 2016-04-06T09:48:24.583 回答