表单提交数据后,我需要执行一个存储过程。我让存储过程按照我想要的方式工作,并且我的表单工作正常。我只是不知道执行laravel 5中的sp的语句。
它应该是这样的:执行mystored_procedure。但我在网上似乎找不到这样的东西。
试试这样的
DB::select('exec my_stored_procedure("Param1", "param2",..)');
或
DB::select('exec my_stored_procedure(?,?,..)',array($Param1,$param2));
尝试无参数
DB::select('EXEC my_stored_procedure')
您也可以这样做:
DB::select("CALL my_stored_procedure()");
对于Laravel 5.4
DB::select(DB::raw("exec my_stored_procedure"));
如果要传递参数:
DB::select(DB::raw("exec my_stored_procedure :Param1, :Param2"),[
':Param1' => $param_1,
':Param2' => $param_2,
]);
适用于Laravel 5.5
DB::select('call myStoredProcedure("p1", "p2")');
或
DB::select('call myStoredProcedure(?,?)',array($p1,$p2));
无参数
DB::select('call myStoredProcedure()')
使用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 ");
如果您正在传递Varchar参数,则使用以下内容:
$submit = DB::select(" EXEC ReturnIdExample '$paramOne', '$paramTwo' ");
如果你只是传递INT或BIGINT的参数,那么这应该有效,你可以从SP:获得返回
$submit = DB::select(" EXEC ReturnIdExample $paramOne,$paramTwo ");
执行存储过程后,值将以数组的形式出现在$submit
中,您需要循环遍历它并访问所需的列。
foreach($submit as $row)
{
echo $row->COLUMN1;
echo $row->COLUMN2;
echo $row->COLUMN3;
}
对于版本5.5,请使用CALL
:
return DB::select(DB::raw('call store_procedure_function(?)', [$parameter]))
经过长时间的研究,这项工作成功了:
DB::connection("sqlsrv")->statement('exec Pro_Internal_Transfer_Note_post @mvoucherid='.$VMID);
app('db')->getPdo()->exec('exec my_stored_procedure');
使用Laraval 5.6、的工作代码
DB::select('EXEC my_stored_procedure ?,?,?',['var1','var2','var3']);
如果您的存储过程总是返回一些东西,那么您可以使用
DB::select("exec StoredProcedure '1','A','PARAM');
否则(如果SP没有响应),它将引发异常。在这种情况下,我建议使用
DB::statetment("exec StoredProcedure '1','A','PARAM'");
带有Laravel 5.6
的MySql(或更高版本可能是)
DB::select(
'call sp($id)'
);
# Real world from my Aplicaction Example Use
$result = DB::connection("sqlsrv")->statement("exec p_SaveOrderWithRelation @cOrderID='{$order->id}', @cPaymentID='{$payment->id}', @cShiftID='{$shift->id}', @cSimple=0");
return $result;
# My PrimaryKey in Models is string type like, and i must put parametr ID like string type:
namespace App'Traits;
use Ramsey'Uuid'Uuid;
use Illuminate'Support'Str;
trait Uuids
{
/**
* Boot function from Laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Str::upper(Uuid::uuid4()->toString());
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return 'string';
}
}
对于从过程中提取的多个语句,请遵循以下代码:
$conn = DB::connection('sqlsrv');
$sql = "exec [sp_name]";
$pdo = $conn->getPdo()->prepare($sql);
$pdo->execute();
$res = array();
do {
array_push($res, $pdo->fetchAll());
} while ($pdo->nextRowset());
echo "<pre />";
print_r($res);
exit();