PDO with MSSQL returns invalid cursor

I am starting a PDO connection on a local MSSQL database. Running any stored procedure at all over a connection gives me no error at all.

This one stored procedure gives me the following error:

Error in SQL: [Microsoft][SQL Server Native Client 10.0]Invalid cursor state - Query: exec sp_Get_SaldosWeb @Tipo=1, @IdDato=15368

      

This is my current PDO line to connect inside this function and return an array:

        $query = $this->db->prepare($qry, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $final = $query->execute();
        $this->setRows($query);
        if(!$final) {
            $this->error($qry, $query, $ret);
        } else {
            return $query->fetchAll();
        }

      

I tried to close the cursor both before execution and after fetchAll:

    $rows = $query->fetchAll();
    $query->closeCursor();
    return $rows;

      

But that doesn't work either. FYI, there are no requests in my code before this call to this stored procedure.

Not sure why this stored procedure is giving so many errors. If I run this exact SP from the MSSQL Management Console it works fine and returns 3 rows.

EDIT:

Here is the stored procedure:

USE [DBNAME]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_Get_SaldosWeb]          
@Tipo int ,  --1 = Alumno 2 = Familia          
@IdDato int           

as          


 if @Tipo = 1           
 begin          

  select SC_Alumno.Codigo ,           
  SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos as [Nombre],          
   SC_Moneda.Nombre as Moneda, upper(replace(replace(replace(replace(replace( replace(lower(SC_TipoCargo.Nombre),char(237),'i'), char(243),'o'), char(233),'e') , char(225),'a') ,char(250),'u'),char(241),'Γ±'))  as [Tipo de Cargo]            
  , cast(round(Sum(SC_CargoxAlumno.Debe),2) as decimal(18,2)) as Monto      ,    
  SC_Alumno.Codigo as Codigo2    
  from SC_CargoxAlumno          
  inner join SC_Moneda on SC_CargoxAlumno.IdSC_Moneda = SC_Moneda.IdSC_Moneda          
  inner join SC_TipoCargo on SC_CargoxAlumno.IdSC_TipoCargo = SC_TipoCargo.IdSC_TipoCargo          
  inner join SC_Alumno on SC_Alumno.IdSC_Alumno = SC_CargoxAlumno.IdSC_Alumno           
  inner join SC_Familia on SC_Alumno.IdSC_Familia = SC_Familia.IdSC_Familia          
  where          
  SC_Alumno.IdSC_Alumno = @IdDato          
  and SC_CargoxAlumno.Debe <> 0          
  group by           
  SC_Alumno.Codigo ,           
  SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos ,          
   SC_Moneda.Nombre , SC_TipoCargo.Nombre        

 end          
 else          
 begin          

  select SC_Alumno.Codigo ,           
  SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos as [Nombre],          
   SC_Moneda.Nombre as Moneda, upper(replace(replace(replace(replace(replace( replace(lower(SC_TipoCargo.Nombre),char(237),'i'), char(243),'o'), char(233),'e') , char(225),'a') ,char(250),'u'),char(241),'Γ±'))  as [Tipo de Cargo]  ,          
  cast(round(Sum(SC_CargoxAlumno.Debe),2) as decimal(18,2)) as Monto      ,    
  SC_Alumno.Codigo as Codigo2    
  from SC_CargoxAlumno          
  inner join SC_Moneda on SC_CargoxAlumno.IdSC_Moneda = SC_Moneda.IdSC_Moneda          
  inner join SC_TipoCargo on SC_CargoxAlumno.IdSC_TipoCargo = SC_TipoCargo.IdSC_TipoCargo          
  inner join SC_Alumno on SC_Alumno.IdSC_Alumno = SC_CargoxAlumno.IdSC_Alumno           
  inner join SC_Familia on SC_Alumno.IdSC_Familia = SC_Familia.IdSC_Familia          
  where          
  SC_Familia.IdSC_Familia = @IdDato          
  and SC_CargoxAlumno.Debe <> 0          
  group by           
  SC_Alumno.Codigo ,           
  SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos ,          
   SC_Moneda.Nombre , SC_TipoCargo.Nombre        

 end

      

+2


source to share


1 answer


Add "SET NOCOUNT ON" to the beginning of your procedure.

You can find links here:



PHP data objects

Best Practices Stored Procedure Checklist

+3


source







All Articles