views:

264

answers:

5

Hello:

I've this cursor declaration:

     DECLARE CursorArticulo CURSOR FOR
        SELECT HstAt.IdArticulo, SUM(HstAt.Cantidad) As SumaCantidad, 
            HstAt.Precio 
        FROM HstArticulosTickets hstAT INNER JOIN HstTickets HstT 
             ON hstAT.IdTicket=hstT.IdTicket
        WHERE hstT.NumUsuarioEmisor=@UsuarioAct
                  AND HstT.NumZona=@ZonaAct
              AND DATEDIFF(day,@par_Fecha,HstT.FechaHoraTicket)=0
  GROUP BY IdArticulo, Precio
  ORDER BY IdArticulo

The parameters @UsuarioAct and @ZonaAct are obtained from another Cursor. The @par_Fecha parameter is an input parameter for a Stored procedure.

If I run the stored procedure, in this cursor I never get a single row. Never enters into the typical WHILE @@FETCH_STATUS = 0 loop.

I try in query analyzer copying the select code and replacing parameters with values and I get the correct rows.

I'm running this in SQL Server 2008.

Why does this happen?

Thank you all.

EDIT:

Full Stored Procedure Code:

ALTER PROCEDURE [dbo].[paCreTablaHojaDeCajaMA] @par_Fecha AS DATETIME AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;

DELETE FROM dbo.TmpDetalleHojaDeCajaDiaria
DELETE FROM dbo.TmpMaestraHojaDeCajaDiaria

INSERT INTO TmpMaestraHojaDeCajaDiaria 
    (NumUsuario, ZonaAsignada, TipoUsuario, NumPDA, ImporteUsuarioZona)
SELECT
    hstZA.NumUsuario, hstZA.NumZonaAsignada, 
       (SELECT TipoUsuario FROM Usuarios U WHERE U.NumUsuario=hstZA.NumUsuario) AS TipoUsuario,
       (SELECT NumPDA FROM Usuarios U WHERE U.NumUsuario=hstZA.NumUsuario) AS NumPDA,
       (SELECT SUM(hstT.ImporteTotal) FROM HstTickets hstT 
            WHERE hstT.NumUsuarioEmisor=hstZA.NumUsuario
            AND hstT.NumZona=hstZA.NumZonaAsignada
            AND DATEDIFF(day,hstZA.Fecha,HstT.FechaHoraTicket)=0) AS ImporteUsuarioZona
FROM hstZonasAsignadas hstZA
WHERE DATEDIFF(day,hstZA.Fecha,@par_Fecha)=0
ORDER BY NumUsuario


DECLARE @UsuarioAct NCHAR(4)
DECLARE @ZonaAct SMALLINT
DECLARE @IdUnicoAct INTEGER
DECLARE @IdArticulo INTEGER
DECLARE @NombreArticulo NCHAR(50)
DECLARE @PrecioUd MONEY
DECLARE @SumaCantidad INTEGER

DECLARE CursorMaestra CURSOR FOR
     SELECT NumUsuario, ZonaAsignada, IdUnico FROM TmpMaestraHojaDeCajaDiaria
   ORDER BY NumUsuario

OPEN CursorMaestra

PRINT 'CURSOR ABIERTO'

-- Vamos a por el primero
FETCH NEXT FROM CursorMaestra INTO @ZonaAct, @UsuarioAct, @IdUnicoAct

WHILE @@FETCH_STATUS = 0
BEGIN

  PRINT @ZonaAct
  PRINT @UsuarioAct

     DECLARE CursorArticulo CURSOR FOR
        (SELECT HstAt.IdArticulo, SUM(HstAt.Cantidad) As SumaCantidad, HstAt.Precio 
        FROM HstArticulosTickets hstAT INNER JOIN HstTickets HstT 
             ON hstAT.IdTicket=hstT.IdTicket
        WHERE hstT.NumUsuarioEmisor=@UsuarioAct
         AND HstT.NumZona=@ZonaAct
                              AND DATEDIFF(day,@par_Fecha,HstT.FechaHoraTicket)=0
      GROUP BY IdArticulo, Precio)

   OPEN CursorArticulo
   PRINT '   CURSOR ABIERTO'

  -- Vamos a por el primero
  FETCH NEXT FROM CursorArticulo INTO @IdArticulo, @SumaCantidad, @PrecioUd

  PRINT @@FETCH_STATUS
     WHILE @@FETCH_STATUS = 0
     BEGIN

   SELECT @NombreArticulo = NombreArticulo FROM Articulos
       WHERE IdArticulo = @IdArticulo

   PRINT @NombreArticulo

   INSERT INTO TmpDetalleHojaDeCajaDiaria
       (NumUsuario, ZonaAsignada, IdArticulo, NombreArticulo, PrecioUD, CantidadZonaUsuario, IdUnicoMaestra)
   VALUES
       (@UsuarioAct, @ZonaAct, @IdArticulo, @NombreArticulo, @PrecioUd, @SumaCantidad, @IdUnicoAct)

   FETCH NEXT FROM CursorArticulo INTO @IdArticulo, @SumaCantidad, @PrecioUd



     END

     CLOSE CursorArticulo
     DEALLOCATE CursorArticulo

     PRINT '   CURSOR CERRADO'

    FETCH NEXT FROM CursorMaestra INTO @ZonaAct, @UsuarioAct, @IdUnicoAct
END

CLOSE CursorMaestra
DEALLOCATE CursorMaestra

PRINT 'CURSOR CERRADO'

END

A: 

Can you try adding parenthesis for select like

DECLARE CursorArticulo CURSOR FOR ( SELECT HstAt.IdArticulo, SUM(HstAt.Cantidad) As SumaCantidad, HstAt.Precio FROM HstArticulosTickets hstAT INNER JOIN HstTickets HstT ON hstAT.IdTicket=hstT.IdTicket WHERE hstT.NumUsuarioEmisor=@UsuarioAct AND HstT.NumZona=@ZonaAct AND DATEDIFF(day,@par_Fecha,HstT.FechaHoraTicket)=0 GROUP BY IdArticulo, Precio ORDER BY IdArticulo )

are the variables @UsuarioAct and @ZonaAct getting populated ? Can you try replacing the same with the one when you tried in manual query?

Umesh
Yes, the variables @UsuarioAct and @ZonaAct are being populated with the rigth values.I'm going to try the parenthesis...
Franklin Albricias
Very curious: When I add the parenthesis I get a "syntax error near ORDER", so I take off the ORDER BY IdArticulo, but I get the same 0 rows. In the manual running there's an ORDER BY and I get no error (repeate: with the correct 4 returning rows).
Franklin Albricias
Is it possible to post the entire query including the other cursor which generates @UsuarioAct and @ZonaAct ?It seems like you are trying a nested cursor and following is one of the links which i refer http://rpbouman.blogspot.com/2005/10/nesting-mysql-cursor-loops.html
Umesh
To Umesh: Nested cursors are allowed in SQL Server. There's even a sample in the docs.
Franklin Albricias
A: 

Are you opening the cursor?

John Nolan
You make me returning to my code quickly, but yes, it's open. :-).
Franklin Albricias
The do you make a ´FETCH NEXT FROM ...´ before you first check for @@FETCH_STATUS? :-)
van
Yes:FETCH NEXT FROM CursorArticulo INTO @IdArticulo, @SumaCantidad, @PrecioUdAnd if I PRINT @IdArticulo I get nothing.
Franklin Albricias
so the value of @@FETCH_STATUS is actually 0 (zero) the first time?
van
what is the value of @@FETCH_STATUS, if it is not 0: -1 or -2?
van
The value of @@FETCH_STATUS is -1.
Franklin Albricias
A: 

Have the variables @UsuarioAct and @ZonaAct been declared as the correct data types?

Maybe an implicit conversion is subtly changing the meaning of the data?

Kev Riley
Hi Kev: I've tested it and all types are correct. Thnx.
Franklin Albricias
A: 

Since you get @@FETCH_STATUS = -1 initially on the inner cursor, I would conclude that your SELECT statement returns no rows. You do mention that you get results when executing the query, but please try the following: the content of the first BEGIN ... END block replace with following code only and see if you do get result sets:

SELECT      HstAt.IdArticulo, SUM(HstAt.Cantidad) As SumaCantidad, HstAt.Precio 
FROM        HstArticulosTickets hstAT 
INNER JOIN  HstTickets HstT 
        ON  hstAT.IdTicket=hstT.IdTicket
WHERE       hstT.NumUsuarioEmisor=@UsuarioAct
        AND HstT.NumZona=@ZonaAct
        AND DATEDIFF(day,@par_Fecha,HstT.FechaHoraTicket)=0
GROUP BY    IdArticulo, Precio

and check if you get any results. If you do not, then remove some of the filters in WHERE clause...

van
I like your way of indenting SQL code. Can I Copy it?
Franklin Albricias
free as a beer... as long as your do not break your team's conding standards
van
A: 

I've found it!

If you read the Outer cursor declaration:

DECLARE CursorMaestra CURSOR FOR SELECT NumUsuario, ZonaAsignada, IdUnico FROM TmpMaestraHojaDeCajaDiaria ORDER BY NumUsuario

The order is NumUsuario, ZonaAsignada, IdUnico

But later, in the first FETCH:

FETCH NEXT FROM CursorMaestra INTO @ZonaAct, @UsuarioAct, @IdUnicoAct

I have changed the order, the correct is @UsuarioAct, @ZonaAct, @IdUnicoAct.

So, the User (UsuarioAct) and Zone (ZonaAct) data was wrong.

Thank you all by your tips.

Franklin Albricias