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:
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.