Click here to Skip to main content
16,004,906 members
Home / Discussions / Database
   

Database

 
QuestionVersion Error on Attaching Database Pin
Tiger45610-Jan-06 23:18
Tiger45610-Jan-06 23:18 
AnswerRe: Version Error on Attaching Database Pin
Dave Kreskowiak11-Jan-06 6:05
mveDave Kreskowiak11-Jan-06 6:05 
QuestionHelp needed in SQL Server connection Pin
satishrg10-Jan-06 17:47
satishrg10-Jan-06 17:47 
AnswerRe: Help needed in SQL Server connection Pin
Colin Angus Mackay10-Jan-06 20:55
Colin Angus Mackay10-Jan-06 20:55 
QuestionDataAdapter.Fill taking forever Pin
Luis Alonso Ramos10-Jan-06 15:59
Luis Alonso Ramos10-Jan-06 15:59 
AnswerRe: DataAdapter.Fill taking for ever Pin
nguyenvhn10-Jan-06 16:21
nguyenvhn10-Jan-06 16:21 
GeneralRe: DataAdapter.Fill taking forever Pin
Luis Alonso Ramos10-Jan-06 17:03
Luis Alonso Ramos10-Jan-06 17:03 
GeneralRe: DataAdapter.Fill taking for ever Pin
Luis Alonso Ramos11-Jan-06 12:43
Luis Alonso Ramos11-Jan-06 12:43 
This is the code that calls the SP:
SqlCommand cmd = new SqlCommand("Reportes_RecoleccionHistoricaDiaria",
    App.GetConnection(false));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID_semana1", weekFrom);
cmd.Parameters.Add("@ID_semana2", weekTo);
 
cmd.CommandTimeout = 300;

SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet("RecoleccionHistoricaDiaria");
da.Fill(ds);
and this is the stored procedure:
CREATE PROCEDURE [dbo].[Reportes_RecoleccionHistoricaDiaria]
(
    @ID_semana1 int,
    @ID_semana2 int
)
AS
BEGIN
 
    DECLARE @Abonos TABLE (ID_semana int, ID_cliente int, Abono int)
    INSERT INTO @Abonos
        SELECT R.ID_semana, R.ID_cliente, MIN(R.ID_abono)
          FROM Recibos R
          WHERE R.Estatus = 1 AND R.ID_semana >= @ID_semana1 AND R.ID_semana <= @ID_semana2
          GROUP BY R.ID_semana, R.ID_cliente
 
    DECLARE @AbonosPorDia TABLE (ID_semana int, DiaPago int, Pagados int)
    INSERT INTO @AbonosPorDia
        SELECT OS.ID_semana, Recibos.DiaPago, COUNT(Recibos.ID_recibo) AS Pagados
          FROM Operacion_Semanas OS
          INNER JOIN Recibos ON OS.ID_semana = Recibos.ID_semana
          INNER JOIN @Abonos A ON Recibos.ID_abono = A.Abono
          WHERE OS.ID_semana >= @ID_semana1 AND OS.ID_semana <= @ID_semana2
          GROUP BY OS.ID_semana, Recibos.DiaPago
          ORDER BY OS.ID_semana
 
    SELECT OS.ID_semana, OS.Fecha,
        (
            SELECT COUNT(ID_recibo)
              FROM Recibos
              WHERE Extra = 0 AND ID_semana = OS.ID_semana
        ) AS Emitidos,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 4
        ), 0)) AS Jueves,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 5
        ), 0)) AS Viernes,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 6
        ), 0)) AS Sabado,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 0
        ), 0)) AS Domingo,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 1
        ), 0)) AS Lunes,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 2
        ), 0)) AS Martes,
        (ISNULL(
            (SELECT Pagados FROM  @AbonosPorDia T
              WHERE T.ID_semana = OS.ID_semana AND T.DiaPago = 3
        ), 0)) AS Miercoles
      FROM Operacion_Semanas OS
      WHERE OS.ID_semana >= @ID_semana1 AND OS.ID_semana <= @ID_semana2
 
END
Basically, I have a table of payments agreed by each customer in table LineasCredito_AbonosPactados which is big and currently contains about 300,000 records. Each week a customer pays one or more receipts (in table Recibos, around 70,000 records), each receipt corresponding to only one payment.

This SP gets how many customers pay each day of the week for weeks starting in @ID_semana1 and ending in @ID_semana2 (each week is a record in Operacion_Semanas).

How it works? The first temporary table gets all the customers that paid at least one receipt, and the day they paid the first receipt, for each week in the range.

The second table counts the customers in the first table for each day of the week, so it ends up with something like this:
Week    Day     Count
---------------------
1        0       100
1        1       123
2        0       122
2        1       134
And finally, the recordset returned contains all the weeks in the range with one field for each different colum:
Week     Total Day 0   Day 1
----------------------------
1         589   100     123 ....
2         628   122     134 ....
This stored procedure works, and relatively well (2 or 3 seconds in very extreme conditions.) But only from SQL Management Studio when called with this code:
EXEC	Reportes_RecoleccionHistoricaDiaria
		@ID_semana1 = 1,
		@ID_semana2 = 100

GO
But if I use the code at the beginning of this post to call the SP from my C# application, then it always times out in the extreme condition (all 40 or so weeks) and takes about a minute for a range of 3 weeks. And then I go to SQL Server Management Studio and click on Execute and it takes less than a second... while the C# app is waiting.

Any ideas? this is driving me crazy!

Thanks!

-- LuisR




Luis Alonso Ramos
Intelectix - Chihuahua, Mexico

Not much here: My CP Blog!


The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005
NewsUpdate - still not working Pin
Luis Alonso Ramos11-Jan-06 15:00
Luis Alonso Ramos11-Jan-06 15:00 
GeneralRe: Update - still not working Pin
nguyenvhn11-Jan-06 21:25
nguyenvhn11-Jan-06 21:25 
AnswerRe: Update - still not working Pin
Luis Alonso Ramos12-Jan-06 6:48
Luis Alonso Ramos12-Jan-06 6:48 
QuestionI cannot update the Data grid fields for the following example: Pin
kenn_rosie10-Jan-06 6:44
kenn_rosie10-Jan-06 6:44 
QuestionODBC - Getting table names ? Pin
Weckmann10-Jan-06 1:50
Weckmann10-Jan-06 1:50 
Question24hr Time Format Pin
tadhg889-Jan-06 23:56
tadhg889-Jan-06 23:56 
AnswerRe: 24hr Time Format Pin
Colin Angus Mackay10-Jan-06 0:19
Colin Angus Mackay10-Jan-06 0:19 
GeneralRe: 24hr Time Format Pin
tadhg8810-Jan-06 1:15
tadhg8810-Jan-06 1:15 
QuestionHelp for sql query(joins) Pin
rs_net9-Jan-06 18:20
rs_net9-Jan-06 18:20 
AnswerRe: Help for sql query(joins) Pin
Colin Angus Mackay9-Jan-06 20:52
Colin Angus Mackay9-Jan-06 20:52 
QuestionWhy the ODP.NET could not be installed? Pin
rushing9-Jan-06 15:25
rushing9-Jan-06 15:25 
QuestionSQL Server DataAdapter Pin
thebison9-Jan-06 10:02
thebison9-Jan-06 10:02 
QuestionRunning SQL server 2000 & 2005 side by side Pin
Tiger4569-Jan-06 1:00
Tiger4569-Jan-06 1:00 
AnswerRe: Running SQL server 2000 & 2005 side by side Pin
Colin Angus Mackay9-Jan-06 1:12
Colin Angus Mackay9-Jan-06 1:12 
GeneralRe: Running SQL server 2000 & 2005 side by side Pin
Tiger4569-Jan-06 1:46
Tiger4569-Jan-06 1:46 
GeneralRe: Running SQL server 2000 & 2005 side by side Pin
Colin Angus Mackay9-Jan-06 20:57
Colin Angus Mackay9-Jan-06 20:57 
GeneralRe: Running SQL server 2000 & 2005 side by side Pin
Tiger45610-Jan-06 17:24
Tiger45610-Jan-06 17:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.