Doubt in Procedure

Asked

Viewed 101 times

-1

Staff made a function in Sqlserver that makes the control of a chart for me in C#, this function brings me all the students of a referent school. The function is as follows:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    ALTER FUNCTION [dbo].[GRF_DESEMPENHO_ANUAL](@P_GMUNICIPIO_ID INT, @P_EDEMPRESA_ID INT,         @P_SECRETARIA_ID INT, @P_ANO CHAR(4), @P_ESCOLA_ID INT, @P_CURSO_ID INT, @P_MODALIDADE_ID    INT, @P_SERIE_ID INT, @P_TURNO_ID INT, @P_TURMA_ID INT, @P_VALOR_ZERO CHAR(1))
    RETURNS @SWT_GRF_DESEMPENHO_ANUAL TABLE
    (
    EDEMPRESA_ID INT,
    EMPRESA CHAR(60),
    GMUNICIPIO_ID INT,
    MUNICIPIO CHAR(40),
    SECRETARIA_ID INT,
    SECRETARIA CHAR(50),
    ANO CHAR(4),
    ESCOLA_ID INT,
    ESCOLA VARCHAR(150),
    CURSO_ID INT,
    CURSO CHAR(100),
    MODALIDADE_ID INT,
    MODALIDADE CHAR(100),
    SERIE_ID INT,
    SERIE_FORMAT CHAR(30),
    TURMA_ID INT,
    LEGENDA CHAR(2),
    TURNO_ID INT,
    TURNO CHAR(20),
    SITUACAO VARCHAR(25),
    TOTAL INT,
    TOTAL_GERAL INT,
    TOTAL_REAL INT
    )
AS
BEGIN 
   DECLARE @V_SERIE_HIST CHAR(3)
   DECLARE @V_GRAU CHAR(2)
   DECLARE @EDEMPRESA_ID INT
   DECLARE @EMPRESA CHAR(60)
   DECLARE @GMUNICIPIO_ID INT
   DECLARE @MUNICIPIO CHAR(40)
   DECLARE @SECRETARIA_ID INT
   DECLARE @SECRETARIA CHAR(50)
   DECLARE @ANO CHAR(4)
   DECLARE @ESCOLA_ID INT
   DECLARE @ESCOLA VARCHAR(150)
   DECLARE @CURSO_ID INT
   DECLARE @CURSO CHAR(100)
   DECLARE @MODALIDADE_ID INT
   DECLARE @MODALIDADE CHAR(100)
   DECLARE @SERIE_ID INT
   DECLARE @SERIE_FORMAT CHAR(30)
   DECLARE @TURMA_ID INT
   DECLARE @LEGENDA CHAR(2)
   DECLARE @TURNO_ID INT
   DECLARE @TURNO CHAR(20)
   DECLARE @SITUACAO VARCHAR(25)
   DECLARE @TOTAL INT
   DECLARE @TOTAL_GERAL INT
   DECLARE @TOTAL_REAL INT
   DECLARE @SWV_cursor_var1 CURSOR

DECLARE @SWV_cursor_var2 CURSOR
      SET @SWV_cursor_var1 = CURSOR  
   FOR SELECT M.ESCOLA_ID, E.ESCOLA, M.SERIE_ID, S.SERIE_HIST, M.TURMA_ID,
           M.TURNO_ID, TN.TURNO, M.EDEMPRESA_ID, M.GMUNICIPIO_ID, M.ANO,
           M.SECRETARIA_ID, EM.EMPRESA, MU.MUNICIPIO, SE.SECRETARIA, S.GRAU,
           S.MODALIDADE_ID, TM.LEGENDA, S.CURSO_ID, CM.MODALIDADE, C.CURSO
   FROM ED_MATRICUL M
   LEFT JOIN ED_ESCOLAS E ON (M.EDEMPRESA_ID = E.EDEMPRESA_ID)
   AND (M.GMUNICIPIO_ID = E.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = E.SECRETARIA_ID)
   AND (M.ESCOLA_ID = E.ESCOLA_ID)
   LEFT JOIN ED_SERIES S ON (M.EDEMPRESA_ID = S.EDEMPRESA_ID)
   AND (M.GMUNICIPIO_ID = S.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = S.SECRETARIA_ID)
   AND (M.SERIE_ID = S.SERIE_ID)
   LEFT JOIN ED_CURSOMODALIDADE CM ON (S.EDEMPRESA_ID = CM.EDEMPRESA_ID)
   AND (S.GMUNICIPIO_ID = CM.GMUNICIPIO_ID) AND (S.SECRETARIA_ID = CM.SECRETARIA_ID)
   AND (S.CURSO_ID = CM.CURSO_ID) AND (S.MODALIDADE_ID = CM.MODALIDADE_ID)
   LEFT JOIN ED_CURSO C ON (S.EDEMPRESA_ID = C.EDEMPRESA_ID)
   AND (S.GMUNICIPIO_ID = C.GMUNICIPIO_ID) AND (S.SECRETARIA_ID = C.SECRETARIA_ID)
   AND (S.CURSO_ID = C.CURSO_ID)
   LEFT JOIN ED_TURMAS TM ON (M.EDEMPRESA_ID = TM.EDEMPRESA_ID)
   AND (M.GMUNICIPIO_ID = TM.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = TM.SECRETARIA_ID)
   AND (M.ESCOLA_ID = TM.ESCOLA_ID) AND (M.SERIE_ID = TM.SERIE_ID)
   AND (M.TURNO_ID = TM.TURNO_ID) AND (M.ANO = TM.ANO) AND (M.TURMA_ID = TM.TURMA_ID)
   LEFT JOIN ED_TURNO TN ON (M.EDEMPRESA_ID = TN.EDEMPRESA_ID)
   AND (M.GMUNICIPIO_ID = TN.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = TN.SECRETARIA_ID)
   AND (M.TURNO_ID = TN.TURNO_ID)
   LEFT JOIN ED_SECRETARIA SE ON (M.EDEMPRESA_ID = SE.EDEMPRESA_ID)
   AND (M.GMUNICIPIO_ID = SE.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = SE.SECRETARIA_ID)
   LEFT JOIN ED_EMPRESA EM ON (SE.EDEMPRESA_ID = EM.EDEMPRESA_ID)
   LEFT JOIN GMUNICIPIO MU ON (SE.GMUNICIPIO_ID = MU.GMUNICIPIO_ID)
   WHERE ((M.GMUNICIPIO_ID = @P_GMUNICIPIO_ID) OR (@P_GMUNICIPIO_ID IS NULL))
   AND ((M.EDEMPRESA_ID = @P_EDEMPRESA_ID) OR (@P_EDEMPRESA_ID IS NULL))
   AND ((M.SECRETARIA_ID = @P_SECRETARIA_ID) OR (@P_SECRETARIA_ID IS NULL))
   AND M.ANO = @P_ANO
   AND ((M.ESCOLA_ID = @P_ESCOLA_ID) OR (@P_ESCOLA_ID IS NULL))
   AND ((S.CURSO_ID = @P_CURSO_ID) OR (@P_CURSO_ID IS NULL))
   AND ((S.MODALIDADE_ID = @P_MODALIDADE_ID) OR (@P_MODALIDADE_ID IS NULL))
   AND ((M.SERIE_ID = @P_SERIE_ID) OR (@P_SERIE_ID IS NULL))
   AND ((M.TURNO_ID = @P_TURNO_ID) OR (@P_TURNO_ID IS NULL))
   AND ((M.TURMA_ID = @P_TURMA_ID) OR (@P_TURMA_ID IS NULL))
   GROUP BY M.ESCOLA_ID,E.ESCOLA,M.SERIE_ID,S.SERIE_HIST,M.TURMA_ID,M.TURNO_ID,TN.TURNO, 
   M.EDEMPRESA_ID,M.GMUNICIPIO_ID,M.ANO,M.SECRETARIA_ID,EM.EMPRESA, 
   MU.MUNICIPIO,SE.SECRETARIA,S.GRAU,S.MODALIDADE_ID,TM.LEGENDA,S.CURSO_ID, 
   CM.MODALIDADE,C.CURSO
   ORDER BY M.EDEMPRESA_ID,M.GMUNICIPIO_ID,M.SECRETARIA_ID,M.ESCOLA_ID,S.CURSO_ID, 
   S.MODALIDADE_ID,M.SERIE_ID,M.TURNO_ID,M.TURMA_ID
   OPEN @SWV_cursor_var1
   FETCH NEXT FROM @SWV_cursor_var1 INTO @ESCOLA_ID,@ESCOLA,@SERIE_ID,@V_SERIE_HIST,@TURMA_ID,@TURNO_ID,@TURNO,
   @EDEMPRESA_ID,@GMUNICIPIO_ID,@ANO,@SECRETARIA_ID,@EMPRESA,@MUNICIPIO,@SECRETARIA, 
   @V_GRAU,@MODALIDADE_ID,@LEGENDA,@CURSO_ID,@MODALIDADE,@CURSO
   while @@FETCH_STATUS = 0
   begin
      SELECT   @SERIE_FORMAT = SERIE_FORMAT FROM SERIE_HISTORICO(@V_SERIE_HIST,@V_GRAU) AS SERIE_HISTORICO

      SET @TOTAL_REAL = 0
      SELECT   @TOTAL_REAL = COUNT(1) FROM ED_MATRICUL M
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '0'

      SET @TOTAL_GERAL = 0
      SELECT   @TOTAL_GERAL = COUNT(1) FROM ED_MATRICUL M
            LEFT JOIN ED_FIC_IND F ON (M.EDEMPRESA_ID = F.EDEMPRESA_ID)
      AND (M.GMUNICIPIO_ID = F.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = F.SECRETARIA_ID)
      AND (M.MATRICULA_ID = F.MATRICULA_ID)
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO not in ('5')


      SET @SWV_cursor_var2 = CURSOR  
      FOR SELECT CAST('Aprovados' AS VARCHAR(25)) AS SITUACAO, COUNT(1) AS TOTAL
      FROM ED_MATRICUL M
      LEFT JOIN ED_FIC_IND F ON (M.EDEMPRESA_ID = F.EDEMPRESA_ID)
      AND (M.GMUNICIPIO_ID = F.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = F.SECRETARIA_ID)
      AND (M.MATRICULA_ID = F.MATRICULA_ID)
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '0'
      AND F.SITU_FINAL = 'S' UNION ALL SELECT CAST('Rep. Desempenho' AS VARCHAR(25)), COUNT(1)
      FROM ED_MATRICUL M
      LEFT JOIN ED_FIC_IND F ON (M.EDEMPRESA_ID = F.EDEMPRESA_ID)
      AND (M.GMUNICIPIO_ID = F.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = F.SECRETARIA_ID)
      AND (M.MATRICULA_ID = F.MATRICULA_ID)
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '0'
      AND F.SITU_FINAL = 'N' UNION ALL SELECT CAST('Rep. Falta' AS VARCHAR(25)), COUNT(1)
      FROM ED_MATRICUL M
      LEFT JOIN ED_FIC_IND F ON (M.EDEMPRESA_ID = F.EDEMPRESA_ID)
      AND (M.GMUNICIPIO_ID = F.GMUNICIPIO_ID) AND (M.SECRETARIA_ID = F.SECRETARIA_ID)
      AND (M.MATRICULA_ID = F.MATRICULA_ID)
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '0'
      AND F.SITU_FINAL = 'F' UNION ALL SELECT CAST('Transf. Rede' AS VARCHAR(25)), COUNT(1)
      FROM ED_MATRICUL M
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '3' UNION ALL SELECT CAST('Transf. Int.' AS VARCHAR(25)), COUNT(1)
      FROM ED_MATRICUL M
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '1' UNION ALL SELECT CAST('Desistente' AS VARCHAR(25)), COUNT(1)
      FROM ED_MATRICUL M
      WHERE M.EDEMPRESA_ID = @EDEMPRESA_ID
      AND M.GMUNICIPIO_ID = @GMUNICIPIO_ID
      AND M.SECRETARIA_ID = @SECRETARIA_ID
      AND M.ANO = @ANO
      AND M.ESCOLA_ID = @ESCOLA_ID
      AND M.SERIE_ID = @SERIE_ID
      AND M.TURNO_ID = @TURNO_ID
      AND M.TURMA_ID = @TURMA_ID
      AND M.SITUACAO = '4' 
 OPEN @SWV_cursor_var2
      FETCH NEXT FROM @SWV_cursor_var2 INTO @SITUACAO,@TOTAL
      while @@FETCH_STATUS = 0
      begin

         IF (@P_VALOR_ZERO = '0')
             BEGIN
                IF (@TOTAL > 0)
                    INSERT INTO @SWT_GRF_DESEMPENHO_ANUAL VALUES(@EDEMPRESA_ID, @EMPRESA, @GMUNICIPIO_ID, @MUNICIPIO, @SECRETARIA_ID, @SECRETARIA, @ANO, @ESCOLA_ID, @ESCOLA, @CURSO_ID, @CURSO, @MODALIDADE_ID, @MODALIDADE, @SERIE_ID, @SERIE_FORMAT, @TURMA_ID, @LEGENDA, @TURNO_ID, @TURNO, @SITUACAO, @TOTAL, @TOTAL_GERAL, @TOTAL_REAL)
             END
         ELSE IF (@P_VALOR_ZERO = '1')

                INSERT INTO @SWT_GRF_DESEMPENHO_ANUAL VALUES(@EDEMPRESA_ID, @EMPRESA, @GMUNICIPIO_ID, @MUNICIPIO, @SECRETARIA_ID, @SECRETARIA, @ANO, @ESCOLA_ID, @ESCOLA, @CURSO_ID, @CURSO, @MODALIDADE_ID, @MODALIDADE, @SERIE_ID, @SERIE_FORMAT, @TURMA_ID, @LEGENDA, @TURNO_ID, @TURNO, @SITUACAO, @TOTAL, @TOTAL_GERAL, @TOTAL_REAL)

         SET @TOTAL_GERAL = 0 
         SET @TOTAL_REAL = 0

         FETCH NEXT FROM @SWV_cursor_var2 INTO @SITUACAO,@TOTAL
      end
      CLOSE @SWV_cursor_var2
      DEALLOCATE @SWV_cursor_var2
      FETCH NEXT FROM @SWV_cursor_var1 INTO @ESCOLA_ID,@ESCOLA,@SERIE_ID,@V_SERIE_HIST,@TURMA_ID,@TURNO_ID,@TURNO,
      @EDEMPRESA_ID,@GMUNICIPIO_ID,@ANO,@SECRETARIA_ID,@EMPRESA,@MUNICIPIO,@SECRETARIA, 
      @V_GRAU,@MODALIDADE_ID,@LEGENDA,@CURSO_ID,@MODALIDADE,@CURSO
   end
   CLOSE @SWV_cursor_var1
   DEALLOCATE @SWV_cursor_var1
RETURN
END

When I Seto 0 for the variable @P_VALOR_ZERO, brings me the correct values, but when I step the value 1 brings wrong value, how do I bring the value 1 the same values when I pass the value 0 ? Detail: when passing the value 0, the chart is displayed only when it has value above 0, when I pass the value 1 it displays everything, even the values = 0, but it adds something when I step value 0 and brings me wrong values.

  • I don’t have time to analyze all the code, but it seems to me that the error is not in this variable but elsewhere. It is only used there in IFs and they are correct. But there are other strange things, for example, this SET @TOTAL_GERAL = 0 lost there in the internal cursor.

  • True, he wouldn’t need to be there, but it doesn’t influence anything, for example, when I pass the value 0, he brings me all the class charts that have more than 0 students to display on the chart, generating for me a total of 279 and real of 196 which is correct, however when I put the value as 1, it brings me the same values and other zeroed graph to show all, however with a general total of 425 and real of 325, that I do not understand the pq, where he makes no sum of anything when I set the value as 1... but thanks for while.

  • The problem is not the parameter. Maybe it is the graph or the total that is adding up the incorrect value. Note that you enter the actual total value and the general even when the total is zero. So, if you add up the total value of all records, it will look different. You need to save the overall and actual total value for each row of the table?

  • It can’t be any different. When I run the process give me the following information Approved - 191 students Dropout - 7 Students Rep. Performance - 5 Students Transf. Int. - 14 Students Transf. Network - 77 Students When I run with parameter 1 it brings me those same lines and one more that is Flunked for Absences - 0 Students, so how it adds different if the values are equal?

1 answer

1

This is because the individual values are on the first cursor, and the totals on the second. Note that this line:

INSERT INTO @SWT_GRF_DESEMPENHO_ANUAL VALUES(@EDEMPRESA_ID, @EMPRESA, @GMUNICIPIO_ID, @MUNICIPIO, @SECRETARIA_ID, @SECRETARIA, @ANO, @ESCOLA_ID, @ESCOLA, @CURSO_ID, @CURSO, @MODALIDADE_ID, @MODALIDADE, @SERIE_ID, @SERIE_FORMAT, @TURMA_ID, @LEGENDA, @TURNO_ID, @TURNO, @SITUACAO, @TOTAL, @TOTAL_GERAL, @TOTAL_REAL)

And this line

FETCH NEXT FROM @SWV_cursor_var2 INTO @SITUACAO,@TOTAL

are independent.

In this case, the logic must be changed so that @TOTAL is also not assigned when the Procedure parameter does not request zeroed values.

  • What I can’t understand is: if (@P_VALOR_ZERO = '0') he makes another condition and inserts it into the table only if IF (@TOTAL > 0), if I do it in the other if (@P_VALOR_ZERO = '1') will work, but it will not show in the graph the zeroed values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.