【问题标题】:How to call MSSQL Stored Procedure with INPUT and OUTPUT parameters in laravelHow to call MSSQL Stored Procedure with INPUT and OUTPUT parameters in laravel
【发布时间】:2022-12-02 11:48:28
【问题描述】:

I'm trying to call MSSQL stored procedure functions in my Laravel project, the procedures with only input parameters work well, but functions with input and output parameters have issues.

when I call the function with only its input parameters to get theBILL_NUMBERlike the below code.

$username="admin";
 $currency_id=1;   
 $results = DB::select( "EXEC stored_procedure_function ?, ?", 
       array($username, $currency_id)
    );

I get this errorSQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Procedure or function 'stored_procedure_function ' expects parameter '@BILL_NUMBER', which was not supplied.

and also when I add the input and output parameters

 $username="admin";
 $currency_id=1;   
 $results = DB::select( "EXEC stored_procedure_function ?, ? , @BILL_NUMBER", 
       array($username, $currency_id)
    );

i get this errorSQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Must declare the scalar variable "@BILL_NUMBER".

【问题讨论】:

    标签: php stored-procedures laravel-7


    【解决方案1】:

    It looks like you are not passing the output parameter @BILL_NUMBER to the stored procedure correctly. In the second example, you are passing it as an input parameter, but it is an output parameter so it needs to be handled differently.

    In order to pass an output parameter to a stored procedure using the Laravel DB facade, you need to use the DB::statement method instead of DB::select, and you also need to specify the output parameter in the $bindings array. Here is an example of how to do that:

    $username = "admin";
    $currency_id = 1;
    
    // Define the output parameter in the bindings array
    $bindings = [
        'username' => $username,
        'currency_id' => $currency_id,
        'BILL_NUMBER' => 0, // This is the output parameter
    ];
    
    // Pass the output parameter to the stored procedure
    $results = DB::statement(
        "EXEC stored_procedure_function @username = :username, @currency_id = :currency_id, @BILL_NUMBER = :BILL_NUMBER OUTPUT",
        $bindings
    );
    

    Note that the output parameter is defined in the $bindings array with a value of 0 (or any other default value). This tells the database that the parameter is an output parameter and sets its initial value. The stored procedure will then assign the final value to this parameter when it is called.

    After calling the stored procedure, you can access the output parameter value by accessing the $bindings array. For example, you can use the following code to get the value of the @BILL_NUMBER output parameter:

    $bill_number = $bindings['BILL_NUMBER'];

    note: I don't have MSSQL on hands to test this, please let me know if it works

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2013-12-29
      • 2022-12-27
      • 2022-12-02
      • 2022-12-01
      • 2022-12-28
      • 2022-12-02
      • 2022-12-26
      • 2022-12-02
      相关资源
      最近更新 更多