Doubt when mounting a Where in a Procedure

Asked

Viewed 354 times

0

On the site, I have a filter that already works. This filter will be maintained, but the table will be another, from another bank including.
The rule for the filter is, if the parameter passed is:

  • Equal to 1, brings only the open.
  • Equal to 2, brings the paid.
  • Equal to 3, brings everything.

So the rules of aberto and pago sane:

E2_VALOR > 0  # (ABERTO)
E2_VALOR = 0  # (PAGO)

The point is how I pass this on the PROC select Where. Below the PROC

DECLARE
@CodTerceiro int = 243664,  
@DataVencimentoIni datetime = null,   
@DataVencimentoFim datetime = null,   
@Pago  tinyint = 3 

SELECT pw.IdPedidoWeb,  
    pw.IdPedido, 
    Convert(DateTime,receber.E1_EMISSAO,103) as 'DataEmissao' ,  
    Convert(DateTime,receber.E1_VENCTO, 103) as 'DataVencimento',     
    pedido.C5_XIDNF AS 'IdNotaFiscal',    
    notafiscal.F2_DOC AS NroNotaFiscal, 
    receber.E1_VALOR AS VlrTotalNotaFiscal,

    CASE WHEN receber.E1_SALDO > 0
    THEN 'Em Aberto'
    ELSE 'Pago'
    END 'Status',

    pw.CodProduto,    
    pw.QtdItem,    
    pw.QtdItem * pw.VlrVenda 'VlrProduto'    
FROM [dbo].[PedidoWeb] pw    
    WITH (NOLOCK, INDEX (Ind_PedidoWeb_2)) 
    INNER JOIN Totvs12.dbo.SC5010 pedido
    WITH (NOLOCK)
    ON pedido.C5_XPEDSND = pw.IdPedido

    INNER JOIN Totvs12.dbo.SE1010 receber 
    WITH (NOLOCK)
    ON receber.E1_FILIAL = pedido.C5_FILIAL
    and receber.E1_NUM = pedido.C5_NOTA

    INNER JOIN Totvs12.dbo.SF2010 notafiscal
    ON notafiscal.F2_DOC = pedido.C5_NOTA
    AND notafiscal.F2_FILIAL = pedido.C5_FILIAL
    AND notafiscal.F2_FILIAL = receber.E1_FILIAL

    WHERE pw.CodTerceiro = 243664    
    AND Convert(DateTime,receber.E1_VENCTO, 103) >= coalesce (@DataVencimentoIni, Convert(DateTime,receber.E1_VENCTO, 103))    
    AND Convert(DateTime,receber.E1_VENCTO, 103) <= coalesce (@DataVencimentoFim, Convert(DateTime,receber.E1_VENCTO, 103))    
    --AND (   @Pago = 3    
    --    OR @Pago = CASE WHEN receber.E1_SALDO > 0 THEN 1 ELSE 2 END)

    --AND ( ( @pago = 1 and receber.E1_SALDO > 0)
    --        Or
    --        ( @pago = 2 and receber.E1_SALDO = 0)
    --        Or
    --        ( @pago = 3) )



    AND pw.IdPedidoWEb between '9000000' and '20000000' 
    --222794
    --SELECT CASE WHEN receber.E1_SALDO > 0
    --THEN 'Em Aberto'
    --ELSE 'Pago'
    --END 'Status' FROM TOTVS12.DBO.SE1010 receber WHERE E1_NUM = '2368332'

and this is the old query, which I am having to replace, because the Notafiscal table will no longer be used, everything will come from PROTHEUS

DECLARE
@CodTerceiro int = 243664,  
@DataVencimentoIni datetime = null,   
@DataVencimentoFim datetime = null,   
@Pago  tinyint = 3


SELECT pw.IdPedidoWeb,  
             pw.IdPedido,  
             cr.DataEmissao,   
             pc.DataVencimento,   
             nf.IdNotaFiscal,  
             cr.NroNotaFiscal,   
             cr.VlrTotalNotaFiscal,  
             CASE WHEN cr.SaldoAbertoNF > 0   
                  THEN 'Em Aberto'  
                  ELSE 'Pago'  
             END 'Status',  
             pw.CodProduto,  
             pw.QtdItem,  
             pw.QtdItem * pw.VlrVenda 'VlrProduto'  
         FROM [dbo].[PedidoWeb] pw  
            WITH (NOLOCK, INDEX (Ind_PedidoWeb_2))  

         INNER JOIN [dbo].[NotaFiscal] nf  
            WITH (NOLOCK)  
            ON nf.IdPedido = pw.IdPedido  

         INNER JOIN [dbo].[ContasReceber] cr  
            ON cr.idContasReceber = nf.IdContasReceber  

         INNER JOIN [dbo].[ParcelaContasReceber] pc  
            ON pc.idContasReceber = nf.IdContasReceber  
           AND pc.IdParcelaContasReceber = (SELECT min (p1.IdParcelaContasReceber)  
                                            FROM [dbo].[ParcelaContasReceber] p1  
                                            WHERE p1.idContasReceber = nf.IdContasReceber)  
         WHERE pw.CodTerceiro = 243664  
           AND pc.DataVencimento >= coalesce (@DataVencimentoIni, pc.DataVencimento)  
           AND pc.DataVencimento <= coalesce (@DataVencimentoFim, pc.DataVencimento)  
           AND (   @Pago = 3  
                OR @Pago = CASE WHEN cr.SaldoAbertoNF > 0 THEN 1 ELSE 2 END)  

           AND pw.IdPedidoWEb between '9000000' and '20000000'

To query old back 104 records between Paid and Open-ended and the new query brings only 60 records and only Paid, even if I change the filter @Paid to 1. I noticed that records in the query that are as paid, in the query old are like Open-ended

  • What does this stretch? AND pc.DataVencimento >= coalesce (null, pc.DataVencimento) &#xA; AND pc.DataVencimento <= coalesce (null, pc.DataVencimento) !!!

  • It’s wrong text, I copied it wrong. AND pc.DataVencimento >= coalesce (@DataVencimentoIni, pc.DataVencimento) AND pc.DataVencimento <= coalesce (@DataVencimentoFim, pc.DataVencimento)

  • When you ask about a problem in your code, you’ll get better answers if you give people code that they can use to reproduce the problem. See how to create a minimum, complete and verifiable example to use in your question.

1 answer

2

Only the filter, a solution

( ( @pago = 1 and SaldoAbertoNF > 0)
  Or
  ( @pago = 2 and SaldoAbertoNF = 0)
  Or
  ( @pago = 3) )
  • Motta, I’ll give it a try.

Browser other questions tagged

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