2016-03-21 129 views
0

我創建了一個觸發如下後觸發update語句採取

alter trigger sale_Trigger_Update on sale 
after update 
as 
begin 
Declare @old_value varchar(50) 
Declare @new_value varchar(50) 
Declare @sale_id UNIQUEIDENTIFIER 

DECLARE new_cur CURSOR FORWARD_ONLY READ_ONLY LOCAL FOR 
     SELECT saleid 
     FROM INSERTED 

    open new_cur 

      Fetch Next from new_cur into @sale_id 
     while @@FETCH_STATUS = 0 
     Begin 
      set @old_value = (select enddate from deleted where SaleID = @sale_id) 
      set @new_value = (select enddate from inserted where SaleID = @sale_id) 
      insert into zzz (old_value,new_value) values(@old_value,@new_value) 
     end 
     CLOSE new_cur 
    DEALLOCATE new_cur 

    end 

很長一段時間。然後我做了一個更新語句如下

update sale 
    set enddate = null 

Sale表只包含2

和執行繼續無限。

我試圖

update sale 
set enddate = null 
where saleid = 10 

同樣的問題。

然後我強行停止執行。然後檢查sale表和zzz表。沒有發生變化。 我相信遊標中存在一些問題。有人可以展示一些光。

**** ****編輯

其實我需要檢查enddate in deleted is null and enddate in inserted is not null

open new_cur 

       Fetch Next from new_cur into @sale_id 
      while @@FETCH_STATUS = 0 
      Begin 
       set @old_value = (select enddate from deleted where SaleID = @sale_id) 
       set @new_value = (select enddate from inserted where SaleID = @sale_id) 
      if @old_value = null and @new_value != null 
      begin 
       SELECT approval.*, 
(select diag.* 
from diag diag 
where approval.id =diag.id 
FOR XML PATH('diag'), TYPE 
), 
(select ser.* 
from ser ser 
where approval.id =ser.id 
FOR XML PATH('ser'), TYPE 
) 
FROM approval approval, 
where approval.id = 1 
and approval.saleid [email protected] 
FOR XML PATH, ELEMENTS, 
root('Head') 
      end if 
      end 
      CLOSE new_cur 
     DEALLOCATE new_cur 
+0

當你說銷售表只包含兩行,即使你的光標(這是不需要的)不應該採取多time.did您檢查任何鎖定,通過以代替該光標阻塞 – TheGameiswar

+0

啓動簡單加入... – mxix

+0

@mxix其實我需要從插入和刪除enddate來檢查一些條件。 – Sachu

回答

0

關於光標。

你可以用你的扳機代替嗎?

alter trigger sale_Trigger_Update on sale 
after update 
as 
begin 

insert into zzz (old_value,new_value) 
select 
    --i.SalesID, 
    d.enddate, 
    i.enddate 
from inserted i 
inner join deleted d on 
    i.SaleID = d.SaleID 
where 
    d.enddate is null and 
    i.enddate is not null 
end 
+0

如果d.enddate是空的,而且日期是空的,我需要執行另一個查詢..對於特定的saleid ..這就是爲什麼我用光標.. – Sachu

+0

你可以添加到你的問題,請...你不斷增加複雜性= P – mxix

+0

編輯問題..如果條件是正確的,我需要生成一個xml銷售..如果兩行更新兩個XML文件將生成 – Sachu