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
for
Select 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语句实现数据的高效管理和费用自动结算。

被折叠的 条评论
为什么被折叠?



