ALTER procedure [dbo].[sp_presupuestopormes]
@WYear smallint,
@Westado smallint=0 output
as
begin
set nocount on;
insert into cbl_presupuestotmp (numcta,anio,totpre,toteje,totene,totfeb,totmar,totabr,totmay,totjun,totjul,totago,totsep,totoct,totnov,totdic)
(select numcta, @Wyear,
(select sum(totpre) from cbl_presupuestos pr where pr.numcta=ca.numcta and pr.anio=@WYear) as totpre,
(select (sum(impdeb)-sum(impcre)) from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear) as toteje,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=1),0) as ene,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=2),0) as feb,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=3),0) as mar,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=4),0) as abr,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=5),0) as may,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=6),0) as jun,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=7),0) as jul,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=8),0) as ago,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=9),0) as sep,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=10),0) as oct,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=11),0) as nov,
isNull((select (sum(impdeb)-sum(impcre)) as ene from cbl_detalletransacciones tr where tr.numcta=ca.numcta and year(tr.fecdoc)=@WYear and month(tr.fecdoc)=12),0) as dic
from cbl_catalogo ca where codpre=1)
set @Westado = @@ERROR
END