Call stored procedure on laravel using SQL Server with OUTPUT

0

I'm trying to call a stored procedure by Laravel 5.5 but I can not. I'm using SQL Server.

Follow the stored procedure:

declare @CodigoRet int

exec Generator 'LancaContaContabil', @Codigo = @CodigoRet output

Select @CodigoRet

I researched a lot and first tried the simplest way:

$results = DB::select(DB::raw('DECLARE @CodigoRet INT; execute Generator 
\'LancaContaContabil\', @Codigo = @CodigoRet OUTPUT;'));
echo $results;

The above code gives the following error: "The active result for the query contains no fields".

I also tried with the statement, but it only returns 1, it follows below:

$results = DB::statement('DECLARE @CodigoRet INT; EXEC Generator 
\'LancaContaContabil\', @Codigo = @CodigoRet OUTPUT;');
echo $results;
die;

I also tried creating the procedure and calling it, but it gives the same problem ("The active result for the query contains no fields"), as if it had no data returning from the select:

$results = DB::select('EXECUTE testeproc');
echo $results;
die;

I'm doing something wrong or is there an easier way to call this stored procedure in laravel?

    
asked by anonymous 27.09.2017 / 15:26

1 answer

0

I just got it sorted out. I will leave here the solution that can help other people with the same problem.

$dbh = DB::connection()->getPdo();
$sth = $dbh->prepare("SET NOCOUNT ON; EXEC ProcLancaContaContabil");
$sth->execute();
$codigo = $sth->fetchAll(PDO::FETCH_COLUMN, 0);

The same trick was to put the "SET NOCOUNT ON" before calling the procedure.

I found it here

    
29.09.2017 / 20:23