create table borrowbook( --建立表學生借書
borrowbook int identity(1,1),
stutid int ,
stufeeid int ,
borrowdate datetime,
returndate datetime,
fee money
)create table stufee( --學生費用結算表
stufeeid int primarykey ,
stuid int ,
borrowbookallfee money,
) --宣告乙個游標
declare curstufee cursor
forselect stufeeid from studentfee
--宣告兩個費用變數
declare @mborrowbookallfee money --總費用
declare @istufeeid int --借書結算號
--初始化
set @mborrowbookallfee=0
set @istufeeid=0
--開啟游標
open curstufee
--迴圈並提取記錄
fetch next from curstudentfee into @istudentfeeid
while ( @@fetch_status=0 )
begin
--從借書記錄中計算某一學生的借書總記錄的總費用
select @mborrowbookallfee=sum(fee)
from borrowbook
where stufeeid=@istufeeid
--更新到彙總表。
update stufee set borrowbookallfee=@mborrowbookallfee
where stufeeid=@istudnetfeeid
fetch next from curstufee into @mfee
end--關閉游標
close curstufee
--釋放游標
deallocate curstufee
SQL Server中游標的使用
舉個栗子 臨時變數 declare iduniqueidentifier 宣告游標名 declare cursor name cursor forselect id from com datadictionaryinfo 開啟游標 open cursor name 先查詢一次再迴圈,防止有多個游標時...
SQL Server 中游標的使用
sql server 中游標的使用 1.游標是行讀取,占用資源比sql多 2.游標的使用情景 現存的系統中使用的是游標,查詢必須通過游標來實現 用盡了while 子查詢臨時表 表變數 自定義函式以及其他方式仍然無法實現的時候,使用游標 3.t sql 中游標的生命週期由5部分組成 定義游標 游標的定...
SQL Server游標的使用
declare cursor name cursor local global forward only scroll static keyset dynamic fast forward read only scroll locks optimistic type warning for sele...