I have a Stored Procedured which creates a temporary table (#test), fills it with data from another table, runs 3 selects on this temporal table and drops it.

The original table it's over 20 GBs big and the 3 SELECT statements contain a lot of different conditions on the original SP.

I'm executing the SP from PHP using SQLSRV however I can only manage to retrieve the 2 first result sets.

If I run the SP from MSSMS it will run just fine and returns the 3 resultsets as expected. But from PHP it will just return 2 (tried every combination).

Not sure if this is a problem with the driver, sqlsrv_fetch_array or sqlsrv_next_result.

Example of the SP (the select statements are too big so I'll just resume them):

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

Now from PHP this is my test code:

$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

However though, if I try this it works:

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

Same thing with this combination:

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

Am I doing something wrong here? Or is there a way to fetch the 3 resultsets from a single SP.

Thanks in advance.

有帮助吗?

解决方案

I was actually just having a similar issue and managed to get the following to work:

$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);

The do-while loop will advance through all results (rather than having to do this manually). It seems that looping through sqlsrv_fetch_array() is essential so I think this is the real answer.

其他提示

// 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;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top