您的位置:首页 > 其它

自定义公式的计算处理

2008-04-26 22:36 260 查看
原帖地址: http://community.csdn.net/Expert/topic/3485/3485588.xml?temp=.8813745
--示例数据
createtablesale(datedatetime,codevarchar(10),amtint)
insertsaleselect'2004-10-22','aa',15000
union all select'2004-10-22','bb',18000
union all select'2004-10-22','cc',20000
union all select'2004-10-23','aa',21000
union all select'2004-10-23','bb',18500
union all select'2004-10-23','cc',19600

createtabledept(codevarchar(10),namevarchar(10))
insertdeptselect'aa','中餐厅'
union all select'bb','西餐厅'
union all select'cc','客房部'
union all select'dd','KTV部'

createtablecost(datedatetime,codevarchar(10),amtint)
insertcostselect'2004-10-22','aa',5000
union all select'2004-10-22','bb',7000
union all select'2004-10-22','cc',11000
union all select'2004-10-23','aa',12000
union all select'2004-10-23','bb',8500
union all select'2004-10-23','cc',9600

createtablemeans(codevarchar(10),seqint,[desc]varchar(10),formulanvarchar(4000))
insertmeansselect'aa',1,'销售收入','^sale@amt^'
union all select'aa',2,'销售成本','^cost@amt^'
union all select'aa',3,'上交利润','([1]-[2])*0.3'
union all select'aa',4,'净利润' ,'[1]-[2]-[3]'
go

/*--问题处理要求描述

写一个计算的存储过程,完成根据各基础资料及计算方法,将数据放到利润明细表中!

如用户输入的查询条件是部门代号aa(@code),日期为:2004-10-22(@date)

则处理过程如下,
1:从利润计算方法表(means)读取code=aa到临时表
select*into#tempfrommeanswherecode=@codeorderbyseq

2:用游标循环处理每一个项目,按seq从小到大的顺序,如第一个项目
insertintogain_detail(code,date,desc,amt)
select@code,@date,@desc,sale.amtfromsalewherecode=@codeanddate=@date
其它项目类似

公式说明:
1.^表名@字段名^:例如:^sale@amt^表示从sale表取amt字段的值,取值条件是code=@codeanddate=@date
2.[seq] :例如:[1]-[2]-[3],[]之间为引用本部门前面的计算结果项,[1]表示是本部门的销售收入[2]表示销售成本,其它类似
3.其他的是标准的计算表达式
--*/

--问题处理:
--公式计算的存储过程
createprocp_calc
@formulanvarchar(4000),--要计算的公式
@codevarchar(10), --部门代码
@datedatetime, --计算的日期
@amtintout --计算的结果
as
declare@s1nvarchar(4000),@s2nvarchar(4000),@iint,@jint

--外部计算
set@i=patindex('%^%@%^%',@formula)
while@i>0
begin
select@j=charindex('@',@formula,@i)
,@s1=substring(@formula,@i,@j-@i)
,@s2='from'
+substring(@formula,@i+1,@j-@i-1)
+'wherecode=@codeanddate=@date'
,@i=charindex('^',@formula,@j)
,@s1=@s1+substring(@formula,@j,@i-@j+1)上一页
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: