5

我有一个存储过程,它创建一个临时表(#test),用另一个表中的数据填充它,在这个临时表上运行 3 个选择并删除它。

原始表超过 20 GB,3 个 SELECT 语句在原始 SP 上包含许多不同的条件。

我正在使用 SQLSRV 从 PHP 执行 SP,但是我只能设法检索 2 个第一个结果集。

如果我从 MSSMS 运行 SP,它将运行良好并按预期返回 3 个结果集。但是从 PHP 中它只会返回 2(尝试了每个组合)。

不确定这是驱动程序 sqlsrv_fetch_array 还是 sqlsrv_next_result 的问题。

SP 的示例(选择语句太大,所以我将恢复它们):

CREATE PROCEDURE sp_transfers
@dt date,
@campaign varchar(16)
AS
BEGIN
CREATE TABLE #test ( [column definitions...] )

BEGIN
INSERT INTO #test SELECT * FROM sometable WHERE dt = @dt AND campaign = @campaign

SELECT * FROM #test ...
SELECT * FROM #test ...
SELECT * FROM #test ...

DROP TABLE #test
END

现在从 PHP 这是我的测试代码:

$q = sqlsrv_query($conn,"EXEC sp_transfers @dt='2013-10-01',@campaign='1234'");

sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); //OK - data as expected - 1st resultset
sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); //OK - data as expected - 2nd resultset
sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); // EMPTY

但是,如果我尝试这样做,它会起作用:

sqlsrv_next_result($q);
sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); //OK - data as expected - 2nd resultset
sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); // OK - 3rd resultset shows up

这个组合也是一样的:

sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); //OK - data as expected - 1st resultset
sqlsrv_next_result($q);
sqlsrv_next_result($q);
var_dump(sqlsrv_fetch_array($q)); // OK - 3rd resultset shows up

我在这里做错了吗?或者有没有办法从单个 SP 中获取 3 个结果集。

提前致谢。

4

2 回答 2

10

我实际上只是遇到了类似的问题并设法使以下工作:

$result = array();

// Get return value
do {
   while ($row = sqlsrv_fetch_array($query)) {
       // Loop through each result set and add to result array
       $result[] = $row;
   }
} while (sqlsrv_next_result($query));

print_r($result);

do-while 循环将遍历所有结果(而不是必须手动执行此操作)。似乎遍历 sqlsrv_fetch_array() 是必不可少的,所以我认为这是真正的答案。

于 2013-10-03T23:50:10.100 回答
-1
// this example for storing the data in drop down menu  - php

   // need to connect data base first
    $connectionInfo = array( "Database"=>$database, "UID"=>DATABASE_USER, "PWD"=>DATABASE_PASSWORD);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    ///  $SQLquery  =  NOTE write sql query here

    $stmt = sqlsrv_query( $conn, $SQLquery );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        } else {
            Display("SQLquery  executed");
        }
        $result = array();  
          $fetchLimit = 0;  // control the infinite loop 
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

                //echo $row['COLUMN_NAME']. "<br>";
                // Loop through each result set and add to result array
                $result[] = $row['COLUMN_NAME'];          

           // results store here  = you need to change  'COLUMN_NAME' based on yoru sql query received data name

                $fetchLimit++;
                if($fetchLimit>60000)
                break;
        }
        echo dropdown( "test", $result, 1000 );



    add this function for storing in drop down 

    function dropdown( $name, array $options, $selected=null )
    {
        /*** begin the select ***/
        $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";

        $selected = $selected;
        /*** loop over the options ***/
        foreach( $options as $key=>$option )
        {
            /*** assign a selected value ***/
            $select = $selected==$key ? ' selected' : null;

            /*** add each option to the dropdown ***/
            $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
        }

        /*** close the select ***/
        $dropdown .= '</select>'."\n";

        /*** and return the completed dropdown ***/
        return $dropdown;
    }
于 2014-12-15T09:33:46.220 回答