关于游标更新了一个时间索引字段之后。。

abcdef133 2013-01-30 09:17:19
对几条数据进行更新,每次更新时间都必须在上次时间上多加10毫秒(updatetime字段),然后另一个字段(updatecount)+1,ps:updatetime字段上建了非聚集索引。

结果读出几条updatecount<20的数据用游标进行逐条更新的时候

declare @j=10
update 表 set updatecount=updatecount+1,updatetime=dateadd(ms,@j,getdate())
set @j=@j+10


每次都更新了N次,updatecount都直接到20.
当把updatetime的非聚集索引去掉的时候,更新就一切正常了。

求解,时间索引和游标有冲突吗?

或求更好的办法。。




...全文
208 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcdef133 2013-01-30
  • 打赏
  • 举报
回复
嗯,果然行。。谢谢了。我去找下动态游标和静态游标的区别
abcdef133 2013-01-30
  • 打赏
  • 举报
回复
刚看到,我先试下
昵称被占用了 2013-01-30
  • 打赏
  • 举报
回复
原来 print @jobid 出来的是否有重复?
昵称被占用了 2013-01-30
  • 打赏
  • 举报
回复
Declare cur_Depart Cursor For select job_id from job_data where Job_UpdateCount<20 and job_estate=1 and job_del=0 and Com_ID=@Com_ID order by job_time asc 修改为 Declare cur_Depart Cursor STATIC For select job_id from job_data where Job_UpdateCount<20 and job_estate=1 and job_del=0 and Com_ID=@Com_ID order by job_time asc 试下
abcdef133 2013-01-30
  • 打赏
  • 举报
回复
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[sp_UpdateAllJob] @Com_ID int AS BEGIN declare @jobid int declare @j int set @j=10 Declare cur_Depart Cursor For select job_id from job_data where Job_UpdateCount<20 and job_estate=1 and job_del=0 and Com_ID=@Com_ID order by job_time asc Open cur_Depart Fetch Next From cur_Depart into @jobid While @@Fetch_Status=0 Begin update Job_Data set Job_UpdateCount=Job_UpdateCount+1,Job_Time=dateadd(ms,@j,getdate()) where job_id=@jobid set @j=@j+10 Fetch Next From cur_Depart into @jobid print @jobid End Close cur_Depart Deallocate cur_Depart END 其中Job_Time 是非聚集索引
昵称被占用了 2013-01-30
  • 打赏
  • 举报
回复
游标代码写全来 游标有很多种打开方式,其中如果动态游标,不排除在游标更新循环中出现一条数据被修改多次,造成你说的现象

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧