How to execute Stored Procedure from Laravel
93,479
Solution 1
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')
Solution 2
You can also do this:
DB::select("CALL my_stored_procedure()");
Solution 3
for Laravel 5.5
DB::select('call myStoredProcedure("p1", "p2")');
or
DB::select('call myStoredProcedure(?,?)',array($p1,$p2));
no parameter
DB::select('call myStoredProcedure()')
Solution 4
for 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,
]);
Solution 5
For version 5.5 use CALL
:
return DB::select(DB::raw('call store_procedure_function(?)', [$parameter]))
Author by
Jordan Davis
Updated on July 09, 2022Comments
-
Jordan Davis almost 2 years
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.
it should be something like this: execute my_stored_procedure. but I can not seem to find anything like that online.
-
Jordan Davis over 8 yearsI do not need to return anything, and my stored procedure does not need any parameters. so could I do DB::select('my_stored_procedure') ?
-
Pரதீப் over 8 years@JordanDavis - check now
-
Jordan Davis over 8 yearsThanks, it now tries to run the SP, however it looks like my server does not have the needed permissions to do so
-
Pரதீப் over 8 yearsIts your
login
which don't have permission toexecute
any thing inServer
. Contact your DBA -
Rogier about 8 years@JordanDavis If you don't need anything returned from the stored procedure, you can also use DB::statement instead of DB::select
-
KD.S.T. about 6 yearsmy error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXEC rule_4' at line 1 (SQL: EXEC rule_4)
-
Munim Munna almost 6 yearsIs this laravel answer?
-
Roshana Pitigala almost 6 yearsWhile this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
-
moses toh almost 6 years@Pரதீப் Maybe you can help me. Look at this : stackoverflow.com/questions/51838922/…
-
zetta over 5 years@MunimMunna of course it is, app() is a Laravel Helper.
-
byaruhaf over 4 yearsWhile this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value
-
Avnish alok over 4 years@byaruhaf Thanks for your suggestion. I've added the scenario to use it.