zoukankan      html  css  js  c++  java
  • 简单使用游标插入数据

    --创建数据库

    create proc InsertStudent

    as

    --定义所需要的变量

    declare @SchoolID int
    declare @ClassID int
    declare @StudentID int
    declare @IDNumber int  --条件判断时需要

    begin

    --创建游标

    declare FeeInsertStudent cursor for 
    select stuID,SchID,ClasID from T_School 

    --打开游标

    open FeeInsertStudent 

    --从游标里取出数据给 变量 赋值

    fetch next from FeeInsertStudent into @SchoolID,@ClassID,@StudentID

    --判断有标的状态

    while @@FETCH_STATUS=0

    begin

    --为变量赋值

    set @IDNumber=(select count(*) from T_LeaveSchool where StudentID=@StudentID and SchoolID=@SchoolID and ClassId=@ClassID)
    if(@IDNumber=0)  --判断
    begin
    insert into T_LeaveSchool(StudentID,ClassID,SchoolID)
    values( @StudentID,@ClassID,@StudentID)
    end
    fetch  next from FeeInsertStudent into @SchoolID,@ClassID,@StudentID
    end
    close FeeInsertStudent         --关闭游标
    deallocate FeeInsertStudent                 --撤销游标


    end

  • 相关阅读:
    语言基础
    进制转换
    添加
    查找
    继承
    封装
    面向基础 c#小复习
    主外键
    三个表的关系
    插入信息,模糊查询,聚合函数,时间函数,排序,字符串函数,数学函数,求个数,球最大
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/3305476.html
Copyright © 2011-2022 走看看