mardi 28 août 2018

Laravel Model SQL Server: Get Output Parameters from Stored Procedure

I want to get the output parameter from my SQL Server stored procedure in the Model of my Laravel, The Stored Procedure is working perfectly in the SQL Server Management tool and also in NORMAL php file but it does not work in LARAVEL MODEL.

This is how I am executing the Stored Procedure in the Laravel Model:

    $id              =   "123454";
    $email           =   "ABC@ABC.com";
    $name            =   "FName, LName";
    $returnval        =   1; //My OUTPUT PARAMETER from SP
    $action       =   'ACTION_MESSAGE;
    $dummy            =   0;
    $client          =   $request->input('client');
    $team_l          =   $request->input('team');
    $contact         =   $request->input('contact');
    $team            =   $request->input('team_l');

    $Query        =  DB::select
                            (" SET NOCOUNT ON; SET ANSI_NULLS ON; SET ANSI_WARNINGS ON; EXEC [MY_STORED_PROC]
                                        $client,
                                        $team,
                                        $contact,
                                        '$team_l',
                                        '$id',
                                        '$name',
                                        '$email',
                                        '$action',
                                        $dummy,
                                        $returnval
                            ");

In Laravel I am getting following error when I try to run the Stored Procedure:

(3/3) QueryException
SQLSTATE[IMSSP]: The active result for the query contains no fields.

This is how I was using in the Normal PHP which was working:

    $id              =   "123454";
    $email           =   "ABC@ABC.com";
    $name            =   "FName, LName";
    $returnval       =   1; //My OUTPUT PARAMETER from SP
    $action          =   'ACTION_MESSAGE;
    $dummy           =   0;
    $client          =   $_POST('client');
    $team            =   $_POST('team');
    $contact         =   $_POST('contact');
    $team_l          =   $_POST('team_l');

    $Query        = "{ CALL  My_PROC_NAME(?,?,?,?,?,?,?,?,?,?) }";
    $PARAMS           = array(   array($client, SQLSRV_PARAM_IN),
                                array($team,        SQLSRV_PARAM_IN),
                                array($contact, SQLSRV_PARAM_IN),
                                array($team_l,      SQLSRV_PARAM_IN),
                                array($id,         SQLSRV_PARAM_IN),
                                array($name,       SQLSRV_PARAM_IN),
                                array($email,       SQLSRV_PARAM_IN),
                                array($action,      SQLSRV_PARAM_IN),
                                array($dummy,       SQLSRV_PARAM_IN),
                                array($returnval,   SQLSRV_PARAM_OUT)
                            );              

        $result     =   sqlsrv_query($connect, $Query,$PARAMS);

My Stored Procedure and everything seems to be fine. I tried the following things keeping in the stored procedure as well but no luck:

SET NOCOUNT ON; 
SET ANSI_NULLS ON; 
SET ANSI_WARNINGS ON;

Do I have to SELECT the return parameter in the SP so we can loop and get it in the LARAVEL MODEL.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire