0

i have this code in my sql server stored procedure, this one recive the data and stored it in a table, when i run it manually it works:

ALTER PROCEDURE [dbo].[sp_registrarsalidavehiculo]

    @est_id as int,
    @uni_patente as varchar(10),
    @uni_id as varchar(20),
    @vig_conductor varchar(50),
    @vig_acompanante as varchar(50),
    @vig_destino as varchar(200),
    @vig_salidaKm as numeric(10, 0),
    @vig_observaciones as varchar(200),
    @com_id as int,
    @usu_id_salida as int,
    @vig_tipo as int,
    @vig_dni  as nvarchar(10)

AS
BEGIN

    insert into rrhh.vigilancia(vig_fechaSalida, est_id, uni_patente, uni_id, vig_conductor, vig_acompanante, vig_destino, vig_salidaKm, vig_observaciones, com_id, usu_id_salida, vig_tipo, vig_dni)
    values(Getdate(), @est_id, @uni_patente, @uni_id, @vig_conductor, @vig_acompanante, @vig_destino, @vig_salidaKm, @vig_observaciones, @com_id, @usu_id_salida, @vig_tipo, @vig_dni)

END

when i try to send data through postman to prove my apirest it show me this error

enter image description here

this is the code in my route file "api.php"

Route::resource('vigilancia', 'App\Http\Controllers\Vigilancia\VigilanciaController', ['only'=>['index','store']]);

this is the code in my controller

public function store(Request $request)
    {
        $est_id             =   $request->est_id;
        $uni_patente        =   $request->uni_patente;
        $uni_id             =   $request->uni_id;
        $vig_conductor      =   $request->vig_conductor;
        $vig_acompañante    =   $request->vig_acompañante;
        $vig_destino        =   $request->vig_destino;
        $vig_salidaKm       =   $request->vig_salidaKm;
        $vig_observaciones  =   $request->vig_observaciones;
        $com_id             =   $request->com_id;
        $usu_id_salida      =   $request->usu_id_salida;
        $vig_tipo           =   $request->vig_tipo;
        $vig_dni            =   $request->vig_dni;


        $est_id             =   ($est_id   ==  NULL) ? ($est_id   =   0) :   $est_id;
        $uni_patente        =   ($uni_patente   ==  NULL) ? ($uni_patente   =   '') :   $uni_patente;
        $uni_id             =   ($uni_id   ==  NULL) ? ($uni_id   =   '') :   $uni_id;
        $vig_conductor      =   ($vig_conductor   ==  NULL) ? ($vig_conductor   =   '') :   $vig_conductor;
        $vig_acompañante    =   ($vig_acompañante   ==  NULL) ? ($vig_acompañante   =   '') :   $vig_acompañante;
        $vig_destino        =   ($vig_destino   ==  NULL) ? ($vig_destino   =   '') :   $vig_destino;
        $vig_salidaKm       =   ($vig_salidaKm   ==  NULL) ? ($vig_salidaKm   =   0) :   $vig_salidaKm;
        $vig_observaciones  =   ($vig_observaciones   ==  NULL) ? ($vig_observaciones   =   '') :   $vig_observaciones;
        $com_id             =   ($com_id   ==  NULL) ? ($com_id   =   0) :   $com_id;
        $usu_id_salida      =   ($usu_id_salida   ==  NULL) ? ($usu_id_salida   =   0) :   $usu_id_salida;
        $vig_tipo           =   ($vig_tipo   ==  NULL) ? ($vig_tipo   =   0) :   $vig_tipo;
        $vig_dni            =   ($vig_dni   ==  NULL) ? ($vig_dni   =   0) :   $vig_dni;
    
DB::select("EXEC sp_registrarsalidavehiculo (". $est_id .", ".$uni_patente.", ".$uni_id.", ".$vig_conductor.", ".$vig_acompañante.", ".$vig_destino.", 
                                                             ".$vig_salidaKm.",".$vig_observaciones.", ".$com_id.", ".$usu_id_salida.", ".$vig_tipo.", ".$vig_dni.")");
Agustin Coronel
  • 297
  • 4
  • 12
  • FYI the prefix `sp_` is reserved, by Microsoft, for **S**pecial / **S**ystem **P**rocedures. It should *not* be used for User Procedures. Doing so comes with a performance cost and the risk of your Procedure simply not working one day after an update/upgrade. Either use a different prefix or (possibly better) no prefix at all. [Is the sp_ prefix still a no-no?](https://sqlperformance.com/2012/10/t-sql-queries/sp_prefix) – Thom A Nov 19 '21 at 14:23
  • Does this answer your question? [How to execute Stored Procedure from Laravel](https://stackoverflow.com/questions/34497063/how-to-execute-stored-procedure-from-laravel) – Charlieface Nov 19 '21 at 14:26
  • Basically you are injecting the parameters directly into the `EXEC` SQL string, instead you should do `EXEC sp_registrarsalidavehiculo ?, ?.....` also no parenthesis `()` needed – Charlieface Nov 19 '21 at 14:27
  • with no parenthesis i have this error "The active result for the query contains no fields. (SQL: exec sp_registrarsalidavehiculo 1, 2, 3, 3, 3, 3, 10000,nada, 3, 3, 3, 9876543 ) in file C:\xampp\htdocs\Laravel\Proyecto\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 703" – Agustin Coronel Nov 19 '21 at 14:33
  • _The active result for the query contains no fields._ Try starting your stored procedure with `set nocount on;` The insert statement returns a rowcount of 1, but with no results. Also you should probably be using `DB::statement` instead of `DB::select` because your stored procedure doesn't return any results at all. – AlwaysLearning Nov 19 '21 at 14:35
  • it doesnt work, now postman doesnt show me nothing, but it doesnt insert the data in the table – Agustin Coronel Nov 19 '21 at 14:50

0 Answers0