我试图从SQL Server 2008中的存储过程(SP)中获取3个结果集,这让我有点想法。
CREATE PROCEDURE db_abbTest
AS
BEGIN
SET NOCOUNT ON;
SELECT 1
SELECT 2
SELECT 3
RETURN 0
END
GO
<?php
$serverName = "server";
$connectionInfo = array("UID"=> "user", "PWD" =>"pass", "Database"=>"sample_db");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
$sql = "db_AbbTest";
$r = sqlsrv_query($conn, $sql, null, array(
'Scrollable' => SQLSRV_CURSOR_STATIC,
'SendStreamParamsAtExec' => true
));
var_dump($r);
while( $obj = sqlsrv_fetch( $r))
{
var_dump($obj);
var_dump(sqlsrv_fetch_array($r));
}
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Close the connection. */
sqlsrv_close( $conn);
SP返回三个结果(3x select语句)但是我似乎无法从查询中获得所有结果。有任何想法吗?
我试过while(sqlsrv_fetch_object($r){//do something})
但没有运气..
答案 0 :(得分:1)
我设法找到了解决这个问题的方法,我不得不使用while循环,然后转到下一个结果:
$sql = "{call db_AbbTest}";
$r = sqlsrv_query($conn, $sql, null);
$i =0;
while( $obj = sqlsrv_fetch_object($r))
{
echo $i;
print_r($obj);
sqlsrv_next_result($r);
$i++;
}