教你怎么使用sql游標(biāo)實(shí)例分享,需要的朋友參考下
[sql]
–1.將每個(gè)老師的工資更新為原來的工資+獎(jiǎng)金
–定義兩個(gè)變量,用來存儲(chǔ)ttid與reward
declare @tid int
declare @reward money
–1。建立一個(gè)基于獎(jiǎng)金表的游標(biāo)
declare cur_reward cursor fast_forward for select ttid,reward from TblTeacherSalary
–2.打開游標(biāo)
open cur_reward
–通過游標(biāo)讀取數(shù)據(jù)
fetch next from cur_reward into @tid,@reward
while @@fetch_status=0
begin
–更新工資
update TblTeacher set ttsalary=ttsalary+@reward where ttid=@tid
fetch next from cur_reward into @tid,@reward
end
–3.關(guān)閉游標(biāo)
close cur_reward
–4.釋放資源
deallocate cur_reward
說明:在一般情況下,不要使用游標(biāo)。性能極點(diǎn)低下。 假如在處理大量數(shù)據(jù)。普通的sql 執(zhí)行非常慢時(shí),這個(gè)時(shí)候可以試試游標(biāo)。也許會(huì)給你帶來意想不到效果
作者 xhccom