Only spend a year in a proc of sql server 2014

Asked

Viewed 48 times

2

I made a filter in a PROC of my own and I’m not getting any results. Running proc did not give any error, but it does not bring any result and I do not know if it is correct. The colleague said I don’t need to pass any parameters, I don’t know if that’s it, but proc is great to put here, but I still posted her code. The field altered by a lot is this: AND YEAR(EXAME.ID_EXM_REA) BETWEEN @DT_DE_LAUDO AND @DT_ATE_LAUDO. I removed the creation of some temporary tables and some Inserts. The desired line is at the end of proc and what I seek is to know if the syntax is correct or not. I removed it because it exceeded the character limit. I left only the header and the part of the return of the Procedure, which is what matters. Too removed for the reason above.

ALTER proc [dbo].[SP_CMO_SEL_PORTARIA_ATIVOS]  

    @ID_XFC_Inicial int,
    @ID_XFC_Final  int,
    @ID_EMP int,
    @ID_UNI_EMP int,
    @ID_XFC_GRP     VarChar( 1000 )= '',
    @DT_ADM DATETIME,
    @DT_DE_LAUDO DATETIME,
    @DT_ATE_LAUDO DATETIME

as
SET NOCOUNT ON  

Begin
.....

/***********************************************************************************************************/
/*************************--ÁREA DE CONSULTA PARA RETORNAR OS DADOS--***************************************/
/***********************************************************************************************************/



    -- Retorna os dados da tabela
    --caso tenha sido definido a lista de funcionarios
    --para exibir, não fazer o filtro por data de admissão
    IF @ID_XFC_GRP <> '' OR @ID_XFC_Inicial <> 0
        BEGIN
            SELECT  #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_UNI_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.RAM_ATV,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DT_LAU_PER,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DT_NASC_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_MAE_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.IR_SEX_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SUS_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_RG_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_CPF_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DS_END,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_CID_END, 
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.SG_UF_END,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_FUN_ULT, 
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_SET_ULT,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_ADM_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_DMS_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_EMP,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_UNI_EMP,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_SET_UNI,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.NM_SET_UNI, 
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.NM_FUN,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_INI_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_FNL_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DS_MAT_CTO,
                    isNull( OIT1980LEITURA.DT_RX, EXAME_RAIOX.DT_EXM_REA ) DT_RX,
                    OIT1980LEITURA.RX_NUM,
                    EXAME_RAIOX.DS_OBS_EXM_REA AS DS_RAIOX,
                    ESPIROMETRIA.CVF_PRE, 
                    ESPIROMETRIA.CVF_MED, 
                    Replace(Cast(ESPIROMETRIA.CVF_POR as varchar(7)), '.', ',') as CVF_POR,  
                    ESPIROMETRIA.CVF_LMT,  
                    ESPIROMETRIA.VEF_PRE,  
                    ESPIROMETRIA.VEF_MED,
                    Replace(Cast(ESPIROMETRIA.VEF_POR as varchar(7)), '.', ',') as VEF_POR,  
                    ESPIROMETRIA.VEF_LMT,
                    ESPIROMETRIA.VEF_CVF_PRE, 
                    ESPIROMETRIA.VEF_CVF_MED, 
                    ESPIROMETRIA.VEF_CVF_POR, 
                    ESPIROMETRIA.VEF_CVF_LMT,
                    ESPIROMETRIA.FEF_PRE,  
                    ESPIROMETRIA.FEF_MED, 
                    Replace(Cast(ESPIROMETRIA.FEF_POR as varchar(7)),'.', ',') as FEF_POR,
                    ESPIROMETRIA.FEF_LMT,
                    EXAME.DS_OBS_EXM_REA AS DS_ESPIROMETRIA,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DS_DTLH_CLSS,
                    CIDADE_UNIDADE.NM_CID_END AS CIDADE 
                    , EXAME.DT_EXM_REA DT_EXA_EXP
                    , NM_EMP,
                    (   Select NO_TEL_UNI_EMP From t_cmo_unidade
                        Where ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP ) Telefone,
                    (   Select DS_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Endereco,
                    (   Select NM_BRR_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Bairro,
                    (   Select CD_CEP From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) CEP,
                    (   Select NM_CID_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Cidade,
                    (   Select SG_UF_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) UF

               FROM #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO 
                    LEFT OUTER JOIN #T_SP_CMO_SEL_REL_FICHA_ADMISSAO    
                    ON #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC = #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_XFC

                    LEFT OUTER JOIN #T_SP_CMO_SEL_REL_FICHA_FUNCAO
                    ON #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC = #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_XFC

                    LEFT OUTER JOIN T_CMO_OIT1980_LEITURA OIT1980LEITURA
                    ON OIT1980LEITURA.ID_OIT_LET = 1
                    AND OIT1980LEITURA.ID_OIT = (SELECT ID_OIT FROM T_CMO_OIT1980 
                                                        WHERE ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                    WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                    AND   ID_EXM = 3936     
                                                                                                    AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0)--NO_SEQ_AVA_LAU_PER, para filtro por avaliação  
                                                                            )
                                            )           
                    LEFT OUTER JOIN T_CMO_EXAME_REALIZADO AS EXAME_RAIOX
                                                             ON  EXAME_RAIOX.NO_SEQ_AVA_LAU_PER = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER     
                                                             AND EXAME_RAIOX.ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC 
                                                             AND EXAME_RAIOX.ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                                  WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                                  AND   ID_EXM = 3936       
                                                                                                                  AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0)--NO_SEQ_AVA_LAU_PER, para filtro por avaliação    
                                                                                            )
                    LEFT OUTER JOIN T_CMO_ESPIROMETRIA ESPIROMETRIA
                                     ON  ESPIROMETRIA.ID_EXP = (SELECT ID_EXP FROM T_CMO_ESPIROMETRIA
                                                                              WHERE ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                                            WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                                            AND   (ID_EXM = 1514 OR ID_EXM = 3136)
                                                                                                                            AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0) --NO_SEQ_AVA_LAU_PER, para filtro por avaliação
                                                                                                    )
                                                                )       
                    --PARA PEGAR A OBS DA ESPIROMETRIA
                    LEFT OUTER JOIN T_CMO_EXAME_REALIZADO AS EXAME
                    ON  EXAME.NO_SEQ_AVA_LAU_PER = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER
                    AND EXAME.ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC        
                    AND EXAME.ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                    WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                    AND   (ID_EXM = 1514 OR ID_EXM = 3136)
                                                                                    AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0) --NO_SEQ_AVA_LAU_PER, para filtro por avaliação
                                                            )                                       
                    left outer join T_CMO_ENDERECO AS CIDADE_UNIDADE
                        ON  CIDADE_UNIDADE.id_emp = @ID_EMP 
                        and CIDADE_UNIDADE.id_uni_emp = @ID_UNI_EMP
                        and CIDADE_UNIDADE.IR_TIP_END = 'U'

             WHERE  #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP = @ID_EMP
                AND #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP = @ID_UNI_EMP
                AND #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC IN (SELECT ID_XFC FROM #FUNCIONARIOS_ATIVOS)
                AND YEAR(EXAME.ID_EXM_REA) BETWEEN @DT_DE_LAUDO AND @DT_ATE_LAUDO

                ORDER BY #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC,
                         #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_ADM_PER_CPC_XFC,
                         #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_INI_PER_FUN_XFC

        END
    ELSE 
        BEGIN
            SELECT  #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_UNI_EMP,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.RAM_ATV,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DT_LAU_PER,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DT_NASC_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_MAE_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.IR_SEX_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SUS_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_RG_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_CPF_XFC,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DS_END,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_CID_END, 
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.SG_UF_END,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_FUN_ULT, 
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NM_SET_ULT,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_ADM_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_DMS_PER_CPC_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_EMP,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_UNI_EMP,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_SET_UNI,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.NM_SET_UNI, 
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.NM_FUN,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_INI_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_FNL_PER_FUN_XFC,
                    #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DS_MAT_CTO,
                    OIT1980LEITURA.DT_RX,
                    OIT1980LEITURA.RX_NUM,
                    EXAME_RAIOX.DS_OBS_EXM_REA AS DS_RAIOX,
                    ESPIROMETRIA.CVF_PRE, 
                    ESPIROMETRIA.CVF_MED, 
                    Replace(Cast(ESPIROMETRIA.CVF_POR as varchar(7)), '.', ',') as CVF_POR,  
                    ESPIROMETRIA.CVF_LMT,  
                    ESPIROMETRIA.VEF_PRE,  
                    ESPIROMETRIA.VEF_MED,
                    Replace(Cast(ESPIROMETRIA.VEF_POR as varchar(7)), '.', ',') as VEF_POR,  
                    ESPIROMETRIA.VEF_LMT,
                    ESPIROMETRIA.VEF_CVF_PRE, 
                    ESPIROMETRIA.VEF_CVF_MED, 
                    ESPIROMETRIA.VEF_CVF_POR, 
                    ESPIROMETRIA.VEF_CVF_LMT,
                    ESPIROMETRIA.FEF_PRE,  
                    ESPIROMETRIA.FEF_MED, 
                    Replace(Cast(ESPIROMETRIA.FEF_POR as varchar(7)),'.', ',') as FEF_POR,
                    ESPIROMETRIA.FEF_LMT,
                    EXAME.DS_OBS_EXM_REA AS DS_ESPIROMETRIA,
                    #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.DS_DTLH_CLSS,
                    CIDADE_UNIDADE.NM_CID_END AS CIDADE 
                    , EXAME.DT_EXM_REA DT_EXA_EXP
                    , NM_EMP,
                    (   Select NO_TEL_UNI_EMP From t_cmo_unidade
                        Where ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP ) Telefone,
                    (   Select DS_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Endereco,
                    (   Select NM_BRR_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Bairro,
                    (   Select CD_CEP From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) CEP,
                    (   Select NM_CID_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) Cidade,
                    (   Select SG_UF_END From t_cmo_endereco 
                        Where IR_TIP_END = 'U' and ID_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP and ID_UNI_EMP = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP    ) UF

               FROM #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO 
                    LEFT OUTER JOIN #T_SP_CMO_SEL_REL_FICHA_ADMISSAO    
                    ON #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC = #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_XFC

                    LEFT OUTER JOIN #T_SP_CMO_SEL_REL_FICHA_FUNCAO
                    ON #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC = #T_SP_CMO_SEL_REL_FICHA_FUNCAO.ID_XFC

                    LEFT OUTER JOIN T_CMO_OIT1980_LEITURA OIT1980LEITURA
                    ON OIT1980LEITURA.ID_OIT_LET = 1
                    AND OIT1980LEITURA.ID_OIT = (SELECT ID_OIT FROM T_CMO_OIT1980 
                                                        WHERE ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                    WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                    AND   ID_EXM = 3936     
                                                                                                    AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0)--NO_SEQ_AVA_LAU_PER, para filtro por avaliação  
                                                                            )
                                            )           
                    LEFT OUTER JOIN T_CMO_EXAME_REALIZADO AS EXAME_RAIOX
                                                             ON  EXAME_RAIOX.NO_SEQ_AVA_LAU_PER = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER     
                                                             AND EXAME_RAIOX.ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC 
                                                             AND EXAME_RAIOX.ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                                  WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                                  AND   ID_EXM = 3936       
                                                                                                                  AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0)--NO_SEQ_AVA_LAU_PER, para filtro por avaliação    
                                                                                            )
                    LEFT OUTER JOIN T_CMO_ESPIROMETRIA ESPIROMETRIA
                                     ON  ESPIROMETRIA.ID_EXP = (SELECT ID_EXP FROM T_CMO_ESPIROMETRIA
                                                                              WHERE ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                                                            WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                                                            AND   (ID_EXM = 1514 OR ID_EXM = 3136)
                                                                                                                            AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0) --NO_SEQ_AVA_LAU_PER, para filtro por avaliação
                                                                                                    )
                                                                )       
                    --PARA PEGAR A OBS DA ESPIROMETRIA
                    LEFT OUTER JOIN T_CMO_EXAME_REALIZADO AS EXAME
                    ON  EXAME.NO_SEQ_AVA_LAU_PER = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER
                    AND EXAME.ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC        
                    AND EXAME.ID_EXM_REA = (SELECT MAX(ID_EXM_REA) FROM T_CMO_EXAME_REALIZADO
                                                                                    WHERE ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC
                                                                                    AND   (ID_EXM = 1514 OR ID_EXM = 3136)
                                                                                    AND   NO_SEQ_AVA_LAU_PER = ISNULL(#T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.NO_SEQ_AVA_LAU_PER, 0) --NO_SEQ_AVA_LAU_PER, para filtro por avaliação
                                                            )                                       
                    left outer join T_CMO_ENDERECO AS CIDADE_UNIDADE
                        ON  CIDADE_UNIDADE.id_emp = @ID_EMP 
                        and CIDADE_UNIDADE.id_uni_emp = @ID_UNI_EMP
                        and CIDADE_UNIDADE.IR_TIP_END = 'U'

             WHERE  #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_EMP = @ID_EMP
                AND #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_UNI_EMP = @ID_UNI_EMP
                AND #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC IN (SELECT ID_XFC FROM #FUNCIONARIOS_ATIVOS)
                AND EXISTS(SELECT ID_XFC FROM #T_SP_CMO_SEL_REL_FICHA_ADMISSAO 
                                        WHERE #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_ADM_PER_CPC_XFC < @DT_ADM
                                    AND #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.ID_XFC = #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC     
                                            )
                AND YEAR(EXAME.ID_EXM_REA) BETWEEN @DT_DE_LAUDO AND @DT_ATE_LAUDO

                ORDER BY #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO.ID_XFC,
                         #T_SP_CMO_SEL_REL_FICHA_ADMISSAO.DT_ADM_PER_CPC_XFC,
                         #T_SP_CMO_SEL_REL_FICHA_FUNCAO.DT_INI_PER_FUN_XFC
        END



    -- Dropa a tabela depois de trazer os dados
    DROP TABLE #T_SP_CMO_SEL_PORTARIA_FUNCIONARIO
    DROP TABLE #T_SP_CMO_SEL_REL_FICHA_ADMISSAO
    DROP TABLE #T_SP_CMO_SEL_REL_FICHA_FUNCAO
    DROP TABLE #MATRICULA
    DROP TABLE #FUNCIONARIOS_ATIVOS

end
  • Your where is saying the following: the 2017 value is between the dates 01/02/2017 and 20/01/2017? I think will not return anything.

  • @pnet: Note that YEAR(EXAME.ID_EXM_REA) returns an integer value, while the parameters @DT_DE_LAUDOand @DT_ATE_LAUDOare declared as datetime. It makes no sense to compare the value of the year with two dates. What is the purpose of the comparison?

  • @Josédiz, what would be the best way for me to pass the parameter? Integer even?

  • @How would I do then? What’s the best way?

  • @pnet I will post as response a suggestion of mine.

  • @pnet: What is the purpose of the comparison? That is, what should be verified.

  • @Hello pnet, consider accepting my answer if it has been helpful to you. If you think she’s incomplete or doesn’t respond to you, make the appropriate comments so I can improve her.

  • 1

    @Ismael, I have already finished my contract. It was a one-off position of only 10 days at most. I will yes, mark and consider as right, looking at the answer. At the time it stayed the way it was and I can’t remember any more details.

Show 3 more comments

1 answer

2


One solution for this filter is to convert it as whole:

DECLARE @ANO_DT_DE_LAUDO INT
DECLARE @ANO_DT_ATE_LAUDO INT

SET @ANO_DT_DE_LAUDO = YEAR(@DT_DE_LAUDO)
SET @ANO_DT_ATE_LAUDO = YEAR(@DT_ATE_LAUDO)

In the clauses where, you change the variable:

AND YEAR(EXAME.ID_EXM_REA) BETWEEN @ANO_DT_DE_LAUDO AND @ANO_DT_ATE_LAUDO

If you can also change the prototype of your trial, it’s another solution:

ALTER proc [dbo].[SP_CMO_SEL_PORTARIA_ATIVOS]  

    @ID_XFC_Inicial int,
    @ID_XFC_Final  int,
    @ID_EMP int,
    @ID_UNI_EMP int,
    @ID_XFC_GRP     VarChar( 1000 )= '',
    @DT_ADM DATETIME,
    @DT_DE_LAUDO INT,
    @DT_ATE_LAUDO INT

as
SET NOCOUNT ON  

Begin
.....

And on the call, you will spend the year directly:

EXEC [dbo].[SP_CMO_SEL_PORTARIA_ATIVOS]  ..., YEAR(DT_DE_LAUDO), YEAR(DT_ATE_LAUDO)

Browser other questions tagged

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