光标没有更新我的列

时间:2013-09-16 11:53:58

标签: sql-server-2008-r2

我正在使用游标来更新我的空列,但它没有更新

Declare @Itemcode as varchar(30)
Declare @SLV as varchar(20)
declare cursor1 cursor for 
Select Itemcode ,U_SLV from  oitm_Clone where sapitem is null and u_slv is null
open cursor1 
  fetch next FROM cursor1 INTO @Itemcode ,@SLV
   WHILE @@FETCH_STATUS = 0
   begin
   if @SLV is null
   begin
   declare @sql1 as varchar(max)        
          set @sql1 = 'Update [dbo].oitm_Clone set sapitem ='''+ @Itemcode +''' where ItemCode='''+ @Itemcode +''' and U_SLV ='''+ @SLV +''''
          print @sql1
          execute (@sql1)
   end
    FETCH NEXT FROM cursor1 INTO @Itemcode ,@SLV
   end
    CLOSE cursor1
DEALLOCATE cursor1

2 个答案:

答案 0 :(得分:1)

我认为您不需要游标来执行此更新。尝试这个简单的更新,它具有与光标相同的逻辑,并且性能大大提高。

Update [dbo].oitm_Clone 
Set sapitem = itemcode
Where sapitem is null and u_slv is null

答案 1 :(得分:0)

尝试使用此

WHILE @@FETCH_STATUS <> -1 

而不是

WHILE @@FETCH_STATUS = 0 

希望它会帮助你

问候