怎么写这个存储过程(300分)

  • 主题发起人 主题发起人 苯苯
  • 开始时间 开始时间

苯苯

Unregistered / Unconfirmed
GUEST, unregistred user!
本人正在做一个项目其中用到3张表
原始数据表:
t_chzl 记录所有的原始数据,每个月有新的资料加进该表
t_data_dealed 处理后的表,原始表的数据处理后,放在本表中(而从原始表中删除)
其主要结构如下
t_chzl
t0 char(7)
t1 char(7)
total decimal(10,2)
t_data_dealed
t0 char(7)
t1 char(7)
total decimal(10,2)
oper_date datetime

其中,在这两个表中,以left(t0,3)为标志,形成不同的部门,

现在,要统计出每天每个部门的处理情况,并将每个部门在每天的统计情况总结成一张表
t_branch_day结构如下
area char(3) (等价于上两个表的left(t0,3)
fee_date char(10) 是属于哪一天的数据
fee_total decimal(10,2)
this_left decimal(10,2)
last_left decimal(10,2)

其中,前一天的this_left的值是第二天记录的last_left,,并且,他们的值为原始表中
剩余的TOTAL的和。fee_total为统计出来的数据,(即为t_data_dealed中该日的数据)
this_left的值是原始表中剩余的数据。如果在表中没有当天的记录,
则产生它,
具体有如下数据
X 2000-08-01 100 500 650
X 2000-08-02 650 300 350
A 2000-08-01 1000 2000 3000
A 2000-08-02 3000 1500 1500
现在有如下的数据
t_chzl
A001234 abcdef 100
A002345 gfgfdg 200
A003456 sdfdsf 500
A012345 dsfdsff 1000
.....

如现在对area=A fee_date="2000-08-03"则产生下面的数据
A 2000-08-03 1500 500 1000.

现在,使用Edit来输入相应的日期
Edit1.Text := FormatDateTime('yyyy-mm-dd',now)来查询出数据,我要使用存储过程来做,该怎么办?
SQL server 7 +D5
 
没看懂,listen
 
这样行不行
CREATE PROCEDURE dt_Branch_day_1234
( @area varchar(4),
@date char(10),
@prevdate char(10),
@flag char(1)
)

AS
declare @total_fee decimal(10,2)
declare @delay_total decimal(10,2)
declare @this_left decimal(10,2)
declare @add_new decimal(10,2)
declare @adjust_fee decimal(10,2)
declare @begindate char(19)
declare @enddate char(19)
declare @last_left decimal(10,2)

set @begindate =@date +' 00:00:00'
set @enddate =@date +' 23:59:59'
--set @prevdate = @date -1
set nocount on

if @area='ROOT'
select * from t_Branch_day where fee_date =@date
else
begin
if @flag ='1'
begin
if exists(select * from t_Branch_day where fee_date =@date and area =@area )
delete t_Branch_Day where area = @area and fee_date =@date
end
select @total_fee=sum(total),@delay_total=sum(delay_fee) from t_data_dealed where t0 like Ltrim(Rtrim(@area+'%')) and (oper_date>=@date and oper_date <DateAdd(day,1,@date))
select @this_left=sum(total) from t_chzl where t0 like Ltrim(Rtrim(@area+'%'))
select @last_left=isnull(this_left,0) from t_Branch_day where fee_date =@prevdate and area=@area
select @add_new=sum(add_new) from t_tenday_rpt where area =@area and tenday_no=left(@date,7) and (flag is null)
and (oper_date>=@date and oper_date <DateAdd(day,1,@date))
select @adjust_fee=sum(add_new) from t_tenday_rpt where area =@area and tenday_no=left(@date,7) and flag is not null
and (oper_date>=@date and oper_date <DateAdd(day,1,@date))

insert into t_Branch_day(area, fee_total, delay_total, this_left, add_new, adjust_fee,fee_date)
values(@area,@total_fee,@delay_total,@this_left,@add_new,@adjust_fee,@date)

select fee_total,delay_total,add_new,adjust_fee,this_left,last_left from t_Branch_day where area = @area and fee_date =@date

set nocount off
end
 
你试一试,不对发MAIL 给我:ice.yang@263.net 请讲的清楚一些。多一点数据。

CREATE PROCEDURE [Comput_sp](@Pi_fee_Date [datetime],@pi_area [VarChar(20)])
AS
set xact_abort on

declare @last_I int,@this_I int,@all_i int
begin tran
select top 1 @last_I= last_left from t_branch_day where area like @pi_area order by fee_date desc
select top 1 @this_i=this_left from t_chzl where t0 like @pi_area order by t0 desc
if @last_i - @this_i < 0
begin
select @all_i =@last_i + @this_I
end
else
begin
select @all_i =@last_i - @this_I
end
INSERT INTO [t_branch_day] values (@pi_area,@pi_fee_date,@last_i,@this_i,@all_i)
commit tran













 
上次没发完
CREATE PROCEDURE dt_Branch_day_1234
( @area varchar(4),
@date char(10),
@flag char(1)
)

AS
declare @prevdate char(10)
declare @total_fee decimal(10,2)
declare @delay_total decimal(10,2)
declare @this_left decimal(10,2)
declare @last_left decimal(10,2)

set @prevdate = Convert(char(10),Cast(@date as datetime)-1,20)
set nocount on

select @total_fee=sum(total),@delay_total=sum(delay_fee) from t_data_dealed where t0 like Ltrim(Rtrim(@area+'%')) and (oper_date>=@date and oper_date <DateAdd(day,1,@date))
select @this_left=sum(total) from t_chzl where t0 like Ltrim(Rtrim(@area+'%'))
select @last_left=isnull(this_left,0) from t_Branch_day where fee_date =@prevdate and area=@area
select @add_new=sum(add_new) from t_tenday_rpt where area =@area and tenday_no=left(@date,7) and (flag is null)
and (oper_date>=@date and oper_date <DateAdd(day,1,@date))
select @adjust_fee=sum(add_new) from t_tenday_rpt where area =@area and tenday_no=left(@date,7) and flag is not null
and (oper_date>=@date and oper_date <DateAdd(day,1,@date))

insert into t_Branch_day(area, fee_total, delay_total, this_left, add_new, adjust_fee,fee_date)
values(@area,@total_fee,@delay_total,@this_left,@add_new,@adjust_fee,@date)

select fee_total,delay_total,add_new,adjust_fee,this_left,last_left from t_Branch_day where area = @area and fee_date =@date

set nocount off
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部