各位大哥,帮我优化一下我的存储过程的事务管理!(30分)

  • 主题发起人 主题发起人 aifafa
  • 开始时间 开始时间
A

aifafa

Unregistered / Unconfirmed
GUEST, unregistred user!
create procedure proc_jifensc_5
@kkhcode char(10)
@jifen decimal(9,2)
@beizhu char(20)
AS
BEGIN
/*删除超过六个月的记录*/
if exists(select * from jifensc where riqi<convert(char(10),dateadd(month,-6,getdate()),120))/*检测有没有在六个月之外的记录*/
begin
begin tran
delete jifensc where riqi<convert(char(10),dateadd(month,-6,getdate()),120) /*删除超过六个月的记录*/
if @@error<>0
begin
rollback tran
print('删除数据库内容失败,请联系开发人员!')
return
end
else
commit tran
end
/*增加记录*/
begin tran
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)
if @@error<>0
begin
rollback tran
print('向数据库增加记录失败,请联系开发人员!')
return
end
else
commit tran
/*更新会员档案表*/
begin tran
update cardcust set leijijf=c.jifen from cardcust a,(select kkhcode,sum(jifen)as jifen from jifensc group by kkhcode) c where a.kkhcode=c.kkhcode and a.kkhcode=@kkhcode
if @@error<>0
begin
rollback tran
print('更新数据库会员表失败,请联系开发人员!')
return
end
else
commit tran
/*列出会员积分内容*/
select a.kkhcode,a.kkhname,sum(b.jifen) as jifen,sum(b.jifen)*10 as jine from cardcust a,jifensc b where a.kkhcode=b.kkhcode and a.kkhcode=@kkhcode and b.riqi>=convert(char(10),dateadd(month,-6,getdate()),120) group by a.kkhcode,a.kkhname
return
END
其实我也知道我的事务管理太罗嗦了,可是我怕去掉其中的事务就起不到事务管理的要求,各位大哥帮我优化下!
 
里面有两个事务,可以减少到一个嘛
 
怎么减少? 我对事务管理不是熟悉,能不能把我的代码优化后贴出来,
 
Declare @ErrorCount int
set @ErrorCount = 0

begin tran
/*删除超过六个月的记录*/
delete jifensc where riqi<convert(char(10),dateadd(month,-6,getdate()),120) /*删除超过六个月的记录*/

set @ErrorCount = @ErrorCount + @@error

/*增加记录*/
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)

set @ErrorCount = @ErrorCount + @@error

/*更新会员档案表*/
begin tran
update cardcust set leijijf=c.jifen from cardcust a,(select kkhcode,sum(jifen)as jifen from jifensc group by kkhcode) c where a.kkhcode=c.kkhcode and a.kkhcode=@kkhcode

set @ErrorCount = @ErrorCount + @@error

if @@error<>0
begin
rollback tran
print('更新数据库会员表失败,请联系开发人员!')
return
end
else
commit tran
 
create procedure proc_jifensc_5
( @kkhcode varchar(10),
@jifen decimal(9,2),
@beizhu varchar(20)
)
AS
BEGIN
/*删除超过六个月的记录*/
begin tran
if exists(select * from jifensc where riqi<convert(char(10),dateadd(month,-6,getdate()),120) and kkhcode=@kkhcode)/*检测有没有在六个月之外的记录*/
begin
delete jifensc where riqi<convert(char(10),dateadd(month,-6,getdate()),120) and kkhcode=@kkhcode /*删除超过六个月的记录*/
end
/*增加记录*/
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)
insert into jifensc (kkhcode,riqi,jifen,beizhu) values (@kkhcode,convert(char(10),getdate(),120),@jifen,@beizhu)

/*更新会员档案表*/

update cardcust set leijijf=c.jifen from cardcust a,(select kkhcode,sum(jifen)as jifen from jifensc group by kkhcode) c where a.kkhcode=c.kkhcode and a.kkhcode=@kkhcode
if @@error<>0
begin
rollback tran
print('修改数据库记录失败,请联系开发人员!')
return
end
else
commit tran
/*列出会员积分内容*/
select a.kkhcode,a.kkhname,sum(b.jifen) as jifen,sum(b.jifen)*10 as jine from cardcust a,jifensc b where a.kkhcode=b.kkhcode and a.kkhcode=@kkhcode group by a.kkhcode,a.kkhname
return
END
我改成这样可以吗? 这样能起到只要有一个操作失败就能全部放弃数据回滚吗?
 
多人接受答案了。
 
后退
顶部