1

I have the following stored procedure:

declare @CodigoRet int

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

Select @CodigoRet
  • OBS: the LancaContaContabil is my table and the Codigo is the PK for that table

And I'm trying to call this procedure in Laravel 5.5 but it gives me the error: "The active result for the query contains no fields":

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

Same error using the variant without dbraw:

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

Also tried with statement, but the return is always 1:

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

EDIT: I created the procedure under the name 'testeproc' and now i've tried this, with the same error as above:

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

What am I doing wrong?

3 Answers 3

3

I just got it sorted out. I will post 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 trick is to use "SET NOCOUNT ON" before calling the procedure. I found this here

Sign up to request clarification or add additional context in comments.

Comments

2

try this:

Without parameter:

$results = DB::select('EXEC your_stored_procedure');

Wit parameters

$results = DB::select('exec your_stored_procedure("parameter1", "parameter2",..)');

or

$results = DB::select('exec your_stored_procedure(?,?,..)',array($parameter1,$parameter2));

2 Comments

Thanks for the reply Alessandro, but i've already tried this and the error is the same "The active result for the query contains no fields". Now i've just created a simple procedure to test, with just one select and its working fine. But the output one, still failing.
@Alessandro Minoccheri Maybe you cn help me. Look at this : stackoverflow.com/questions/51838922/…
1

Here is a sample Stored Procedure:

CREATE PROCEDURE ReturnIdExample
    (
             @paramOne int
            ,@paramTwo nvarchar(255)
    )
    AS

    SET NOCOUNT ON; --IMPORTANT!

    BEGIN

    -- Grab the id that was just created
    DECLARE @ObjectID int;

    INSERT INTO [Table]
    (
             [ColumnNameA]
            ,[ColumnNameB]
    ) VALUES (
             @paramOne
            ,@paramTwo
    )

    SET @ObjectID = SCOPE_IDENTITY();

    -- Select the id to return it back to laravel
    SELECT@ObjectID AS ObjectID;

END

Calling this Stored Procedure in Laravel Model/Controller:

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

Accessing the Return Variable in Laravel Model:

return $submit[0]->ObjectId;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.