亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個sql存儲過程循環遍歷賦值的問題

一個sql存儲過程循環遍歷賦值的問題

Smart貓小萌 2018-12-06 17:24:56
要實現的效果是:如果傳進去的@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)--結算額asset nocount on set rowcount 0set 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=0begindeclare @icy intselect @icy=1while @icy<=(select count(cyid) from chengyuan)begindeclare @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>@jiesuaneupdate chengyuan set cyoffer=cyoffer+@jiesuane where cyid=@idelse if @yingfu=@jiesuaneupdate chengyuan set cyoffer=0 ,cyreturn=0 where cyid=@idelse if @yingfu<@jiesuaneupdate chengyuan set cyoffer=@jiesuane-@yingfu ,cyreturn=0 where cyid=@idset @icy=@icy+1endendelse --declare @icy1 int--select @icy1=1if @cyid in (select cyid from chengyuan)beginselect @yingfu=(select cyreturn from chengyuan where cyid=@cyid)-(select cyoffer from chengyuan where cyid=@cyid)if @yingfu>@jiesuaneupdate chengyuan set cyoffer=cyoffer+@jiesuane where cyid=@cyidelse if @yingfu=@jiesuaneupdate chengyuan set cyoffer=0 ,cyreturn=0 where cyid=@cyidelse if @yingfu<@jiesuaneupdate chengyuan set cyoffer=@jiesuane-@yingfu ,cyreturn=0 where cyid=@cyidendcommit transactionif @error_no!=0 return @error_noreturn 0 問題是:其中當@cyid=0時,為什么chengyuan表中cyid=1的那條數據沒有改變,其他的數據均改變了?
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

你的語句需要修改 因為你在執行

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循環內還是循環前,值一樣)

?

查看完整回答
反對 回復 2019-01-07
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

select 、update等SQL語句都是對行同時操作的。如一個表test(a,b)

a   b

1  2

update test set a=b,b=a where a=1后,

結果:

a  b

2  1

查看完整回答
反對 回復 2019-01-07
  • 2 回答
  • 0 關注
  • 1634 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號