How to execute Stored Procedure from Laravel

前端 未结 11 1555
无人及你
无人及你 2020-12-01 00:28

I need to execute a stored procedure after my form submits data. I have the stored procedure working like I want it, and I have my form working properly. I just do not know

相关标签:
11条回答
  • 2020-12-01 00:56

    You can also do this:

    DB::select("CALL my_stored_procedure()");
    
    0 讨论(0)
  • 2020-12-01 00:56

    Running the Microsoft SQL Server Stored Procedure (MS SQL Server) using PHP Laravel framework. If you are trying to run SP using Laravel Model then you can use following two approaches.

    $submit = DB::select(" EXEC ReturnIdExample ?,?", array( $paramOne ,$paramTwo ) ); 
    
    $submit = DB::select(" EXEC ReturnIdExample $paramOne,$paramTwo ");
    

    If incase you are passing the Varchar Parameter then use the following:

    $submit = DB::select(" EXEC ReturnIdExample '$paramOne', '$paramTwo' ");
    

    If you are just passing parameter which are of INT or BIGINT then this should work and you can get the return from SP:

    $submit = DB::select(" EXEC ReturnIdExample $paramOne,$paramTwo ");
    

    Once the stored procedure is executed the values will be present in the $submit in the form of array, you need to loop through it and access the required columns.

    foreach($submit  as $row)
    {
    
    echo $row->COLUMN1;
    echo $row->COLUMN2;
    echo $row->COLUMN3;
    
    }
    
    0 讨论(0)
  • 2020-12-01 00:58

    If your stored procedure always returns something then you can go with

    DB::select("exec StoredProcedure '1','A','PARAM');
    

    Otherwise (if there is no response from the SP) it will throw an exception. In that case I recommend using

    DB::statetment("exec StoredProcedure '1','A','PARAM'");
    
    0 讨论(0)
  • 2020-12-01 01:03

    for Laravel 5.5

    DB::select('call myStoredProcedure("p1", "p2")');
    

    or

    DB::select('call myStoredProcedure(?,?)',array($p1,$p2));
    

    no parameter

    DB::select('call myStoredProcedure()')
    
    0 讨论(0)
  • 2020-12-01 01:03

    Working code with Laraval 5.6,

    DB::select('EXEC my_stored_procedure ?,?,?',['var1','var2','var3']);
    
    0 讨论(0)
提交回复
热议问题