教你怎么使用sql游標實例分享
更新時間:2012年06月26日 19:28:20 作者:
教你怎么使用sql游標實例分享,需要的朋友參考下
[sql]
--1.將每個老師的工資更新為原來的工資+獎金
--定義兩個變量,用來存儲ttid與reward
declare @tid int
declare @reward money
--1。建立一個基于獎金表的游標
declare cur_reward cursor fast_forward for select ttid,reward from TblTeacherSalary
--2.打開游標
open cur_reward
--通過游標讀取數(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)閉游標
close cur_reward
--4.釋放資源
deallocate cur_reward
說明:在一般情況下,不要使用游標。性能極點低下。 假如在處理大量數(shù)據(jù)。普通的sql 執(zhí)行非常慢時,這個時候可以試試游標。也許會給你帶來意想不到效果
作者 xhccom
--1.將每個老師的工資更新為原來的工資+獎金
--定義兩個變量,用來存儲ttid與reward
declare @tid int
declare @reward money
--1。建立一個基于獎金表的游標
declare cur_reward cursor fast_forward for select ttid,reward from TblTeacherSalary
--2.打開游標
open cur_reward
--通過游標讀取數(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)閉游標
close cur_reward
--4.釋放資源
deallocate cur_reward
說明:在一般情況下,不要使用游標。性能極點低下。 假如在處理大量數(shù)據(jù)。普通的sql 執(zhí)行非常慢時,這個時候可以試試游標。也許會給你帶來意想不到效果
作者 xhccom
相關(guān)文章
SQLServer常見數(shù)學函數(shù)梳理總結(jié)
這篇文章主要為大家介紹了SQLServer常見數(shù)學函數(shù)梳理總結(jié)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
SQL server 定時自動備份數(shù)據(jù)庫的圖文方法
這篇文章主要介紹了SQL server 定時自動備份數(shù)據(jù)庫的圖文方法,需要的朋友可以參考下2017-02-02
SQLserver中字符串查找功能patindex和charindex的區(qū)別
CHARINDEX 和 PATINDEX 函數(shù)都返回指定模式的開始位置,PATINDEX 可使用通配符,而 CHARINDEX 不可以2012-05-05
SQL實現(xiàn)篩選出連續(xù)3天登錄用戶與窗口函數(shù)的示例代碼
本文主要介紹了SQL實現(xiàn)篩選出連續(xù)3天登錄用戶與窗口函數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

