要实现的效果是:如果传进去的@cyid=0,则把chengyuan表中所有成员的cyoffer 和cyreturn改变;如果传进去的@cyid!=0,且@cyid是chengyuan表中的成员id,则,改变当前id对应的cyoffer 和cyreturn值。
结算存储过程如下
---------------------------成员结算----------------
create proc sp_jiesuan
@cyid int ,--要结算的成员id,如果id为零则将所有成员结算
@jiesuane numeric(18,2)--结算额
as
set nocount on
set rowcount 0
set transaction isolation level read uncommitted
declare @error_no integer, --错误号
@error_info varchar(255)---错误信息
select @error_no =0,
@error_info =''
select @cyid=isnull(@cyid,0),
@jiesuane =isnull(@jiesuane,0.00)
begin transaction
declare @yingfu numeric(18,2)
if @cyid=0
begin
declare @icy int
select @icy=1
while @icy<=(select count(cyid) from chengyuan)
begin
declare @id int
select @id=(select top 1 cyid from chengyuan where cyid not in(
select top (@icy-1) cyid from chengyuan order by cyid asc) order by cyid asc),
@yingfu= ((select cyreturn from chengyuan where cyid=@id)-(select cyoffer from chengyuan where cyid=@id))
if @yingfu>@jiesuane
update chengyuan set cyoffer=cyoffer+@jiesuane where cyid=@id
else if @yingfu=@jiesuane
update chengyuan set cyoffer=0 ,cyreturn=0 where cyid=@id
else if @yingfu<@jiesuane
update chengyuan set cyoffer=@jiesuane-@yingfu ,cyreturn=0 where cyid=@id
set @icy=@icy+1
end
end
else
--declare @icy1 int
--select @icy1=1
if @cyid in (select cyid from chengyuan)
begin
select @yingfu=(select cyreturn from chengyuan where cyid=@cyid)-(select cyoffer from chengyuan where cyid=@cyid)
if @yingfu>@jiesuane
update chengyuan set cyoffer=cyoffer+@jiesuane where cyid=@cyid
else if @yingfu=@jiesuane
update chengyuan set cyoffer=0 ,cyreturn=0 where cyid=@cyid
else if @yingfu<@jiesuane
update chengyuan set cyoffer=@jiesuane-@yingfu ,cyreturn=0 where cyid=@cyid
end
commit transaction
if @error_no!=0
return @error_no
return 0
问题是:其中当@cyid=0时,为什么chengyuan表中cyid=1的那条数据没有改变,其他的数据均改变了?
你的语句需要修改 因为你在执行
select @id=(select top 1 cyid from chengyuan where cyid not in(
select top (@icy-1) cyid from chengyuan order by cyid asc) order by cyid asc),
@yingfu= ((select cyreturn from chengyuan where cyid=@id)-(select cyoffer from chengyuan where cyid=@id))
这一段的时候 第一次循环时@id在(select cyreturn from chengyuan where cyid=@id )里面为null值的
所以@yingfu 也等于null 当然后面的update语句@id才是第一次查询出来的值
等到第二次循环后在(select cyreturn from chengyuan where cyid = @id) @id等于第一次循环的值后面的update语句@id才是第二次查询出来的值
(注意:不管declare @icy int在循环内还是循环前,值一样)
select 、update等SQL语句都是对行同时操作的。如一个表test(a,b)
a b
1 2
update test set a=b,b=a where a=1后,
结果:
a b
2 1