同个表中,使用触发器使该表中的字段的联动更新

ALTER trigger [A01_UPDATE] on [dbo].[A01]
after update
as
begin

------------------------------------------------------------------------------
    update a01
    set a01.dept_bz=deptcode.dept_code,
       a01.dept_cj=deptcode.parent
    from deptcode, inserted a
    where a01.dept_id =(SELECT I.dept_id
    FROM INSERTED I INNER JOIN DELETED   D  
    ON I.dept_id=D.dept_id)
    and a01.dept_id=deptcode.dept_id

-------------------------------------------------------------------------------

--批量更新

declare @deptID int
   declare @a0188 int
   declare newdeptID cursor for
    select distinct A.dept_id,A.a0188 from inserted A,deptcode
   where A.dept_id=deptcode.dept_id
   OPEN newdeptID
   fetch  next from newdeptID into @deptID,@a0188  --a0188是key field

while   @@fetch_status=0  
  begin  
    update a01
    set a01.dept_bz=deptcode.dept_code,
       a01.dept_cj=deptcode.parent
    from deptcode,inserted a
    where a.dept_id=@deptID and a01.a01110='种类'
          and deptcode.dept_id=a.dept_id
          and a01.a0188=@a0188
    --print @deptID
    fetch  next from newdeptID into @deptID,@a0188
   end
   close newdeptID
   deallocate newdeptID
end;

posted @ 2010-05-10 19:22  cyb  阅读(384)  评论(0编辑  收藏  举报