日期:2014-05-17 浏览次数:20530 次
create proc uf_stock (@dept varchar(50)='',@sdate varchar(20)='',@edate varchar(20)='')
--exec uf_stock '仓库','20130506','20130825'
as begin
declare @consql varchar(max)
declare @sql varchar(max)
set @consql=''
if len(@dept)>0
set @consql=@consql+' and [部门] like ''%'+@dept+'''%'
if len( @sdate )>0
set @consql=@consql+' and [进货日期]>='''+@sdate+''' and [退货日期]>='''+@sdate
+''' and [销售日期]>='''+@sdate+''''
if len(@edate)>0
set @consql=@consql+' and [进货日期]<='''+@edate+''' and [退货日期]<='''+@edate
+''' and [销售日期]<='''+@edate+''''
set @sql='select 货号,sum(a.金额) as 期初金额,sum(b.金额) as 进货金额
,sum(c.金额) as 退货金额,sum(d.金额) as 销售总金额,
sum(e.金额) as 销售退货总金额 from 库存表 a left join (select * from
进货表 where 1=1'+@consql+')b on a. 货号=b.货号
left join (select * from 退货表 where 1=1'+@consql+' )c on a.货号=c.货号
left join (select * from 销售表 where 1=1'+@consql+')d on a.货号=d.货号
left join (select * from 销售退货表 where 1=1'+@consql+')e on a.货号=e.货号
group by 货号'
exec (@sql)
--print @sql
end