sql server 中游标的使用……

 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

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值