Stored Procedure Avançado
Olá amigos,
É o seguinte criei uma Store Procedure para fazer um relatório financeiro, o relatório funciona que é uma beleza mas quando ele é gerado e não tem um valor no receita ou despesa (nulo) ele não mostra o saldo anterior, mas quando tem os valores no receita e no despesa ele mostra certinho (faz o calculo).
Alguém poderia me ajudar, está nesta parte do código.
Codigo Completo:
Rodrigo.
É o seguinte criei uma Store Procedure para fazer um relatório financeiro, o relatório funciona que é uma beleza mas quando ele é gerado e não tem um valor no receita ou despesa (nulo) ele não mostra o saldo anterior, mas quando tem os valores no receita e no despesa ele mostra certinho (faz o calculo).
Alguém poderia me ajudar, está nesta parte do código.
SALDOANTERIOR=(VCRE-VDEB); if (SALDOANTERIOR is null) then SALDOANTERIOR=0;
Codigo Completo:
CREATE PROCEDURE SPREL_BALANC_CC_01 ( DATAINICIO DATE, DATAFIM DATE, CONTA INTEGER, TIPO CHAR(1)) RETURNS ( NOMECENTROCUSTO VARCHAR(50), SALDOANTERIOR NUMERIC(15,2), DATA DATE, DOCUMENTO INTEGER, HISTORICO VARCHAR(60), CODIGOTIPO INTEGER, CREDITOS NUMERIC(15,2), DEBITOS NUMERIC(15,2)) AS DECLARE VARIABLE VCRE NUMERIC(10,2); DECLARE VARIABLE VDEB NUMERIC(10,2); DECLARE VARIABLE VTIPO INTEGER; DECLARE VARIABLE VVALOR NUMERIC(10,2); DECLARE VARIABLE VCONTACCUSTO INTEGER; DECLARE VARIABLE VCONTACRE INTEGER; DECLARE VARIABLE VCONTADEB INTEGER; begin if (:TIPO=´T´ ) then /*TODOS*/ begin for select lan.CONTAPATRIMONIAL, cc.CENTRODECUSTO, lan.DATA, lan.DOCUMENTO, lan.HISTORICO, lan.VALOR, lan.TIPODOLANCAMENTO, lan.TIPODECREDITO, lan.TIPODEDEBITO from lancamentocont lan join CENTRODECUSTO cc on (lan.CENTRODECUSTO=cc.CODIGOCENTROCUSTO) where lan.data >= :datainicio and lan.data <= :datafim order by cc.CENTRODECUSTO, lan.DATA into:VCONTACCUSTO,:NOMECENTROCUSTO,:DATA,:DOCUMENTO,:HISTORICO,:VVALOR,:VTIPO,:VCONTACRE,:VCONTADEB do begin select sum(cre.valor) from lancamentocont cre where cre.data < :datainicio and cre.tipodolancamento = 1 and cre.CENTRODECUSTO = :VCONTACCUSTO into:vcre; select sum(deb.valor) from lancamentocont deb where deb.data < :datainicio and deb.tipodolancamento = 2 and deb.CENTRODECUSTO = :VCONTACCUSTO into:vdeb; SALDOANTERIOR=(VCRE-VDEB); if (SALDOANTERIOR is null) then SALDOANTERIOR=0; if (:VTIPO=1) then begin CODIGOTIPO=:VCONTACRE; CREDITOS=VVALOR; DEBITOS=0; end else if (:VTIPO=2) then begin CODIGOTIPO=:VCONTADEB; DEBITOS=VVALOR; CREDITOS=0; end suspend; end end else if (:TIPO=´S´ ) then /*SELECINADO CONTA*/ begin for select lan.CENTRODECUSTO, cc.CENTRODECUSTO, lan.DATA, lan.DOCUMENTO, lan.HISTORICO, lan.VALOR, lan.TIPODOLANCAMENTO, lan.TIPODECREDITO, lan.TIPODEDEBITO from lancamentocont lan join CENTRODECUSTO cc on (lan.CENTRODECUSTO=cc.CODIGOCENTROCUSTO) where lan.data >= :datainicio and lan.data <= :datafim and lan.CENTRODECUSTO = :CONTA order by cc.CENTRODECUSTO, lan.DATA into:VCONTACCUSTO,:NOMECENTROCUSTO,:DATA,:DOCUMENTO,:HISTORICO,:VVALOR,:VTIPO,:VCONTACRE,:VCONTADEB do begin select sum(cre.valor) from lancamentocont cre where cre.data < :datainicio and cre.tipodolancamento = 1 and cre.CENTRODECUSTO = :VCONTACCUSTO into:vcre; select sum(deb.valor) from lancamentocont deb where deb.data < :datainicio and deb.tipodolancamento = 2 and deb.CENTRODECUSTO = :VCONTACCUSTO into:vdeb; SALDOANTERIOR=(VCRE-VDEB); if (SALDOANTERIOR is null) then SALDOANTERIOR=0; if (:VTIPO=1) then begin CODIGOTIPO=:VCONTACRE; CREDITOS=VVALOR; DEBITOS=0; end else if (:VTIPO=2) then begin CODIGOTIPO=:VCONTADEB; DEBITOS=VVALOR; CREDITOS=0; end suspend; end end end
Rodrigo.
Título editado por Massuda
Rodrigorpb
Curtidas 0
Respostas
Emerson Nascimento
22/03/2006
tente assim:
note que o comando CASE e a função COALESCE só estão disponíveis a partir do Firebird 1.5 e do Interbase 7.5
CREATE PROCEDURE SPREL_BALANC_CC_01 ( DATAINICIO DATE, DATAFIM DATE, CONTA INTEGER, TIPO CHAR(1)) RETURNS ( NOMECENTROCUSTO VARCHAR(50), SALDOANTERIOR NUMERIC(15,2), DATA DATE, DOCUMENTO INTEGER, HISTORICO VARCHAR(60), CODIGOTIPO INTEGER, CREDITOS NUMERIC(15,2), DEBITOS NUMERIC(15,2)) AS DECLARE VARIABLE VCRE NUMERIC(10,2); DECLARE VARIABLE VDEB NUMERIC(10,2); DECLARE VARIABLE VTIPO INTEGER; DECLARE VARIABLE VVALOR NUMERIC(10,2); DECLARE VARIABLE VCONTACCUSTO INTEGER; DECLARE VARIABLE VCONTACRE INTEGER; DECLARE VARIABLE VCONTADEB INTEGER; begin for select (case when :TIPO=´T´ then lan.CONTAPATRIMONIAL else lan.CENTRODECUSTO end), cc.CENTRODECUSTO, lan.DATA, lan.DOCUMENTO, lan.HISTORICO, lan.VALOR, lan.TIPODOLANCAMENTO, lan.TIPODECREDITO, lan.TIPODEDEBITO from lancamentocont lan join CENTRODECUSTO cc on (lan.CENTRODECUSTO=cc.CODIGOCENTROCUSTO) where (lan.data between :datainicio and :datafim) and (:TIPO=´T´ or (:TIPO=´S´ and lan.CENTRODECUSTO = :CONTA)) order by cc.CENTRODECUSTO, lan.DATA into :VCONTACCUSTO,:NOMECENTROCUSTO,:DATA,:DOCUMENTO,:HISTORICO, :VVALOR,:VTIPO,:VCONTACRE,:VCONTADEB do begin select sum((case when tipodolancamento = 1 then coalesce(valor,0) else 0 end)) credito, sum((case when tipodolancamento = 2 then coalesce(valor,0) else 0 end)) debito from lancamentocont where data < :datainicio and CENTRODECUSTO = :VCONTACCUSTO into :vcre, :vdeb; SALDOANTERIOR=(VCRE-VDEB); if (:VTIPO=1) then begin CODIGOTIPO=:VCONTACRE; CREDITOS=coalesce(VVALOR,0); DEBITOS=0; end else if (:VTIPO=2) then begin CODIGOTIPO=:VCONTADEB; DEBITOS=coalesce(VVALOR,0); CREDITOS=0; end suspend; end end
note que o comando CASE e a função COALESCE só estão disponíveis a partir do Firebird 1.5 e do Interbase 7.5
GOSTEI 0
Rodrigorpb
22/03/2006
Obrigado deu certinho,
outro problema em um outro código parecido:
nesta parte do código:
Quando o cre.valor não tiver valor (for nulo), gostaria de que ele recebece 0 (zero) e o deb.valor a mesmo coisa, pois se eles naum tiverem valor algum (mulos) eles naum voltam a conta. como posso colocar para se eles forem nul recebe 0.
Obrigado,
valeu mesmo.
Rodrigo.
outro problema em um outro código parecido:
AS begin if (:TIPO=´T´ ) then /*TODOS*/ begin select ( select sum(cre.valor) from lancamentocont cre where cre.data < :datainicio and cre.tipodolancamento = 1 )- ( select sum(deb.valor) from lancamentocont deb where deb.data < :datainicio and deb.tipodolancamento = 2 ) as saldoanterior, ( select sum(cre.valor) from lancamentocont cre where cre.data >= :datainicio and cre.data <= :datafim and cre.tipodolancamento = 1 ) as creditos, ( select sum(deb.valor) from lancamentocont deb where deb.data >= :datainicio and deb.data <= :datafim and deb.tipodolancamento = 2 ) as debitos
nesta parte do código:
( select sum(cre.valor) from lancamentocont cre where cre.data < :datainicio and cre.tipodolancamento = 1 )- ( select sum(deb.valor) from lancamentocont deb where deb.data < :datainicio and deb.tipodolancamento = 2 )
Quando o cre.valor não tiver valor (for nulo), gostaria de que ele recebece 0 (zero) e o deb.valor a mesmo coisa, pois se eles naum tiverem valor algum (mulos) eles naum voltam a conta. como posso colocar para se eles forem nul recebe 0.
Obrigado,
valeu mesmo.
Rodrigo.
GOSTEI 0
Emerson Nascimento
22/03/2006
a resposta está no tópico acima: use a função COALESCE():
AS begin if (:TIPO=´T´ ) then /*TODOS*/ begin select ( select coalesce(sum(cre.valor),0) from lancamentocont cre where cre.data < :datainicio and cre.tipodolancamento = 1 )- ( select coalesce(sum(deb.valor),0) from lancamentocont deb where deb.data < :datainicio and deb.tipodolancamento = 2 ) as saldoanterior, ( select coalesce(sum(cre.valor),0) from lancamentocont cre where cre.data >= :datainicio and cre.data <= :datafim and cre.tipodolancamento = 1 ) as creditos, ( select coalesce(sum(deb.valor),0) from lancamentocont deb where deb.data >= :datainicio and deb.data <= :datafim and deb.tipodolancamento = 2 ) as debitos
GOSTEI 0
Rodrigorpb
22/03/2006
emerson.en caramba vc sempre me responde todas corretos, afff. Como pode ser tão inteligente.
Valeu pela 100ª vez.
:D
Rodrigo.
Valeu pela 100ª vez.
:D
Rodrigo.
GOSTEI 0