How to improve this sql code?

Asked

Viewed 61 times

0

I would like to improve this code so that it does not repeat the same code in both Ifs. I don’t feel like using procedures because I don’t see the need.

DECLARE @Ordem VARCHAR (7), @Valor VARCHAR(10)
SET @Ordem = 'E'
IF @Ordem = 'E'

BEGIN
  SELECT 
  P.ParcDocFinDupNum,P.ParcDocFinSeq,P.ParcDocFinDesmPag,P.ParcDocFinDataEmissao,P.ParcDocFinDataVenc,
  P.BcoNum,P.TipoCobCod,P.ParcDocFinValOrig,P.ParcDocFinValor, P.DocFinChv,E.EntNome,
  D.EmpCod,D.DocFinTipoLanc,D.DocFinProjecao, D.DocFinChv, D.EntCod, D.DocFinEspec, GetDate() as DataHoje
  FROM DOC_FIN D 
INNER JOIN PARC_DOC_FIN P ON D.EntCod = P.EntCod 
INNER JOIN ENTIDADE E ON E.EntCod = P.EntCod
WHERE  
D.DocFinTipoLanc = 'PAG' 
AND ('Todos' = 'Não' OR D.DocFinProjecao = 'Não')
ORDER BY E.EntNome ASC, RAND()

END

IF @Ordem = 'V'
SELECT 
  P.ParcDocFinDupNum,P.ParcDocFinSeq,P.ParcDocFinDesmPag,P.ParcDocFinDataEmissao,P.ParcDocFinDataVenc,
  P.BcoNum,P.TipoCobCod,P.ParcDocFinValOrig,P.ParcDocFinValor, P.DocFinChv,E.EntNome,
  D.EmpCod,D.DocFinTipoLanc,D.DocFinProjecao, D.DocFinChv, D.EntCod, D.DocFinEspec, GetDate() as DataHoje
  FROM DOC_FIN D 
INNER JOIN PARC_DOC_FIN P ON D.EntCod = P.EntCod 
INNER JOIN ENTIDADE E ON E.EntCod = P.EntCod
WHERE  
D.DocFinTipoLanc = 'PAG' 
AND ('Todos' = 'Não' OR D.DocFinProjecao = 'Não')
ORDER BY convert(varchar,P.ParcDocFinDataVenc,103), E.EntNome ASC

END
  • (1) Is it Mysql or SQL Server? // (2) Evaluate the snippet 'Todos' = 'Não'

  • from what I understand, the only difference between the ifs is the ordination. That’s right?

1 answer

2


Browser other questions tagged

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