declare NVacation cursor for select startTime,endTime,LeaveType from NationalVacation
--NationalVacation表有三条记录
open NVacation
declare @stratTime datetime,@endTime datetime,@LeaveType int
fetch next from NVacation into @stratTime,@endTime,@LeaveType
while @@fetch_status=0
begin
fetch next from NVacation into @stratTime,@endTime,@LeaveType
update EmployeesAttendance set EmployeesStatus=@LeaveType where DateMonths between @stratTime and @endTime
--这条更新语句为啥只更新了一条语句的条件.其它两条没有更新
print @@rowcount
end
close NVacation
deallocate NVacation
------解决方案--------------------
游标本来就是一条一条操作的啊,是不是你的数据有问题?
------解决方案--------------------
貌似你没实现循环
------解决方案--------------------
declare?NVacation?cursor?for?select?startTime,endTime,LeaveType?from?NationalVacation
--NationalVacation表有三条记录
open??NVacation
??
declare?@stratTime?datetime,@endTime?datetime,@LeaveType?int?
??
??
fetch?next?from?NVacation?into?@stratTime,@endTime,@LeaveType
while?@@fetch_status=0
begin
update?EmployeesAttendance?set?EmployeesStatus=@LeaveType?where?DateMonths?between?@stratTime?and?@endTime
print?@@rowcount
fetch?next?from?NVacation?into?@stratTime,@endTime,@LeaveType
end
??
close?NVacation
??
deallocate?NVacation
------解决方案--------------------
其实就像while循环里面一样,当执行完当前的数据之后,要再类似id+1这样,才能继续读下面的数据,不然只会读当前的数据,这个在联机丛书上已经有例子的
------解决方案--------------------
一般来说,用游标的时候,都是这种格式:
declare cur for select * from tb
open cur --打开游标
fetch next from cur into @变量 --取一条数据
while @@fetch_status = 0 --判断游标是否到末尾了
begin
你的操作
fetch next from cur into @变量
end
close cur --关闭游标
deallocate cur --释放游标
------解决方案--------------------
try this,
declare @stratTime datetime,@endTime datetime,@LeaveType int
declare NVacation scroll cursor for
select startTime,endTime,LeaveType
from NationalVacation
open NVacation
fetch first from NVacation into @stratTime,@endTime,@LeaveType
while(@@fetch_status<>-1)
begin
update EmployeesAttendance set EmployeesStatus=@LeaveType where DateMonths between @stratTime and @endTime
print @@rowcount
fetch next from NVacation into @stratTime,@endTime,@LeaveType
end
close NVacation
deallocate NVacation