Skip to content
Advertisement

Laravel multi result set query builder

I have a MSSQL stored procedure :

BEGIN
    SET NOCOUNT ON;
    SELECT [ID],
           [Type],
           [message]
    FROM [ir.pay].[dbo].[notifications]
    WHERE ReciverID = @userID;
    SELECT ID AS id,
           Website AS website,
           Status AS status
    FROM dbo.Customers
    WHERE UserID = @userID;
END;

And i am executing this with my laravel app :

DB::connection('sqlsrv')->select('EXEC StoredProcedureName  @userID=1');

It just returns me the first SELECT query result.

How can i get both result sets?

Advertisement

Answer

Solved by writing bellow function :

function get_multi_result_set($conn, $statement)
{
    $results = [];
    $pdo = DB::connection($conn)->getPdo();
    $result = $pdo->prepare($statement);
    $result->execute();
    do {
        $resultSet = [];
        foreach ($result->fetchall(PDO::FETCH_ASSOC) as $res) {
            array_push($resultSet, $res);
        }
        array_push($results, $resultSet);
    } while ($result->nextRowset());

    return $results;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement