php 如何从 Laravel 执行存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34497063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:02:47  来源:igfitidea点击:

How to execute Stored Procedure from Laravel

phpsql-serverlaravelstored-procedureslaravel-5

提问by Jordan Davis

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 the statement to execute the sp from laravel 5.

我需要在表单提交数据后执行存储过程。我让存储过程像我想要的那样工作,而且我的表单工作正常。我只是不知道从 laravel 5 执行 sp 的语句。

it should be something like this: execute my_stored_procedure. but I can not seem to find anything like that online.

它应该是这样的:执行 my_stored_procedure。但我似乎无法在网上找到类似的东西。

回答by P?????

Try something like this

尝试这样的事情

DB::select('exec my_stored_procedure("Param1", "param2",..)');

or

或者

DB::select('exec my_stored_procedure(?,?,..)',array($Param1,$param2));

Try this for without parameters

不带参数试试这个

DB::select('EXEC my_stored_procedure')

回答by Roque Mejos

You can also do this:

你也可以这样做:

DB::select("CALL my_stored_procedure()");

回答by Pasindu Jayanath

for Laravel 5.4

对于Laravel 5.4



DB::select(DB::raw("exec my_stored_procedure"));


if you want to pass parameters:

如果要传递参数:

DB::select(DB::raw("exec my_stored_procedure :Param1, :Param2"),[
    ':Param1' => $param_1,
    ':Param2' => $param_2,
]);

回答by vanquan223

for Laravel 5.5

适用于 Laravel 5.5

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

or

或者

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

no parameter

无参数

DB::select('call myStoredProcedure()')

回答by BATMAN

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.

使用 PHP Laravel 框架运行 Microsoft SQL Server 存储过程 (MS SQL Server)。如果您尝试使用 Laravel 模型运行 SP,那么您可以使用以下两种方法。

$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:

如果您要传递 Varchar 参数,请使用以下内容:

$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:

如果您只是传递 INT 或 BIGINT 的参数,那么这应该可以工作,您可以从 SP 获得返回:

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

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

执行存储过程后,值将以$submit数组的形式出现,您需要遍历它并访问所需的列。

foreach($submit  as $row)
{

echo $row->COLUMN1;
echo $row->COLUMN2;
echo $row->COLUMN3;

}

回答by Faisal

After a long research, this works:

经过长时间的研究,这有效:

DB::connection("sqlsrv")->statement('exec Pro_Internal_Transfer_Note_post @mvoucherid='.$VMID);

回答by Avnish alok

MySql with Laravel 5.6(or above version may be)

MySql with Laravel 5.6(或以上版本可能)

DB::select( 'call sp($id)' );

DB::select('调用 sp($id)');

回答by zetta

app('db')->getPdo()->exec('exec my_stored_procedure');

回答by DTIndia

Working code with Laraval 5.6,

Laraval 5.6 的工作代码,

DB::select('EXEC my_stored_procedure ?,?,?',['var1','var2','var3']);

回答by iMezied

For version 5.5 use CALL:

对于 5.5 版使用CALL

return DB::select(DB::raw('call store_procedure_function(?)', [$parameter]))