fuelingtheweb's avatar

Running Stored Procedures

Can anybody help me get stored procedures working right with Laravel / PHP? I'm connecting to a SQL Server database and I've got the procedure mostly working, but I can't get the out parameters to be returned no matter what I try. Below is what I have working at the moment. The procedure runs properly, but the $resultId and $wasTableUpdated variables remain set to 0 instead of updating with the out parameters from the procedure. Any help would be greatly appreciated.

$trainingRecordId = 5318852;
$score = 86;
$completionDate = '10/31/2014';

$db = DB::connection('vision');

$returnId = 0;
$wasTableUpdated = 0;

$stmt = $db->getPdo()->prepare("EXEC dbo.prUpdatePostScoreAndCompDate ?, ?, ?, ?, ?");

$stmt->bindParam(1, $trainingRecordId);
$stmt->bindParam(2, $score);
$stmt->bindParam(3, $completionDate);
$stmt->bindParam(4, $returnId, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(5, $wasTableUpdated, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);

echo $returnId . "\n";
$stmt->execute();
echo $returnId;
0 likes
21 replies
uxweb's avatar

Think that for execute a sp you could use DB::sentence("EXEC dbo.uspMyProcedure(1, 2, 3);").

Hope this helps!

fuelingtheweb's avatar

Thank you for the input. I'm actually not having any issues running a stored procedure in Laravel. My problem is getting the out parameters after the stored procedure runs. In my example, $returnId and $wasTableUpdated are supposed to be updated by the stored procedure but it's not working. Everything I've ready online says binding the out parameters like I have is supposed to do the trick, but I can't get it to work.

thepsion5's avatar

Have you tried doing the same thing with just PDO and not the query builder? It may be something specific to PDO and/or SQL Server and not Laravel-related at all.

Amrit01's avatar
 \DB::select('EXEC dbo.prUpdatePostScoreAndCompDate ?, ?, ?, ?, ?', array($trainingRecordId,$score,$completionDate,$returnId,$wasTableUpdated));

try this way . not tested yet...

ritey's avatar

I've always found with PHP and MSSQL the driver installed matters a lot! Also typical the recordset(s) returned need to be closed to then be able to access any return or input/output parameters

Davids's avatar

Try this, output parameters &$returnId


$stmt->bindParam(4, &$returnId, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(5, &$wasTableUpdated, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
bgarrison25's avatar

I would be interested in seeing a solution to this as well. Did any of the above end up working for you OP?

fuelingtheweb's avatar

No, none of the above worked. You can't pass in &$returnId because you get this error: "Call-time pass-by-reference has been removed."

ritey's avatar

Just to return to this, I found adding $stmt->nextRowset(); after the execute line then released the output parameters in the stored procedure I was working with. Also ensure SET NOCOUNT ON is within the stored procedure.

JayEdgar's avatar

ritey, how did you define the output parameters in your stored procedure? Thanks much.

kumarsujendra's avatar

I tried below and it works for me. $statement = DB::connection()->getPdo()->prepare("CALL sp_updateOwnerDetails(?)"); $statement->bindParam(1, $id);

You need to do changes like below $stmt = $db->getPdo()->prepare("CALL prUpdatePostScoreAndCompDate ?, ?, ?, ?, ?");

gustavo11star's avatar

$data = DB::select("SET NOCOUNT ON exec dbo.mystoredprocedure ?,?,?;", ['20180101', '20180130','G']);

I used this way and obtained results

roberlay's avatar

Hi, maybe you could find the solution ?, I am having the same problem.

luilliarcec's avatar

Currently tested in laravel 6.*, this is the solution, add the following instruction "SET NOCOUNT ON" before executing the stored procedure, however I find it necessary to pass a variable with faker value in my case null, in the position of the output parameter.

CREATE PROCEDURE sp_gestion_clientes
@Id INT, 
@Nombres VARCHAR(50),
@Cedula VARCHAR(13),
@Direccion VARCHAR(150),
@Option VARCHAR(10),  
@Mensaje VARCHAR(255) OUT
AS
BEGIN TRY
    BEGIN TRAN

    IF @Option = 'INSERT'
    BEGIN
        INSERT Cliente VALUES(@Nombres, @Cedula, @Direccion)
        SET @Mensaje = 'Se insertó el registro exitosamente'
    END

    SELECT @Mensaje AS 'Mensaje'
...
$data = \Illuminate\Support\Facades\DB::select('SET NOCOUNT ON exec sp_gestion_clientes ?, ?, ?, ?, ?, ?', array('0', 'Luis Arce', '1234567890', 'Ecuador', 'INSERT', null));

dd($data);

The result is as follows

array:1 [▼
  0 => {#246 ▼
    +"Mensaje": "Se insertó el registro exitosamente"
  }
]
1 like
Xfaider's avatar

For this test procedure:

CREATE PROCEDURE testOutput
  @param uniqueidentifier OUTPUT
AS
BEGIN
   SET @param = NEWID()
END

working solution:

$query = '
   DECLARE @id uniqueidentifier
   EXEC testOutput @id output
   select @id as id
';

DB::select($query);

output:

array:1 [
  0 => {#452
    +"id": "48B0175E-7FD6-481E-A2D8-819FCEB83BEC"
  }
]

Please or to participate in this conversation.