游标使用

    技术2025-11-28  6

    --游标使用--创建A表,保存工号KeyId及薪水codecreate table A(          KeyId int,          code int)--创建B表,保存工号及涨薪create table B(   KeyId int,   codeadd int)--分别为A,B表添加两条数据insert into A values(1,100)insert into A values(2,200)insert into B values(1,10)insert into  B values(2,20)--声明游标,从B表中取出涨薪数据declare mycursor cursor for select * from B--打开游标open mycursor--声明变量,用于存放游标读取的数据declare @KeyId varchar(10),        @CodeAdd int--取出第一条数据fetch next from mycursor into @KeyId,@Codeadd--加上涨薪数据update A set code=code+codeAdd where KeyId=@KeyId--循环取出数据while(@@fetch_status=0)beginfetch next from mycursor into @KeyId,@CodeAddend--关闭游标close mycursor--删除游标deallocate mycursor

    最新回复(0)