CREATE TRIGGER  Dele_Pers_Code
ON A001
instead of delete
AS
  begin
      begin tran 
          declare  @tablename varchar(4) 
          declare  table_cursor cursor for
          SELECT   Table_physical    FROM  x_control_table   WHERE (X_kinds_id = 'X') AND (Table_id <> 50) AND (Table_id <> 1)
          open table_cursor
          fetch next from table_cursor into @tablename
         while @@fetch_status=0
 begin
                  exec(' delete from '+@tablename+' where pers_code in (select pers_code from deleted) ')
                   FETCH NEXT FROM table_cursor into @tablename
              end
        close table_cursor
        DEALLOCATE table_cursor
        if @@error!=0  
                  rollback tran
        else commit tran
  
      
  end
触发器报错:deleted对象找不到,什么原因?

解决方案 »

  1.   

    INSTEAD OF DELETE Triggers
    INSTEAD OF DELETE triggers can be defined on a view or table to replace the standard action of the DELETE statement. Usually, the INSTEAD OF DELETE trigger is defined on a view to modify data in one or more base tables.DELETE statements do not specify modifications to existing data values. DELETE statements specify only the rows that are to be deleted. The inserted table passed to a DELETE trigger is always empty. The deleted table sent to a DELETE trigger contains an image of the rows as they existed before the UPDATE statement was issued. In the case of an INSTEAD OF DELETE trigger on a view or table, the format of the deleted table is based on the format of the select list defined for the view
      

  2.   

    在 exec 中执行时是无法使用 deleted 表的,deleted 表只能在触发器内部的语句中使用。exec 执行时已和触发器不属于同一个事务级别了!去 SQL Server 版看看有没有这方面的信息。
      

  3.   

    exec(' delete from '+@tablename+' where pers_code in (select pers_code from deleted) ')
    不用Exec也可以
    但怎么将@tablename作为一个表名参数呢
      

  4.   

    如果是写触发器,是不是可以不用表名参数,而使用Deleted和inserted表。
      

  5.   

    declare  @tablename  sysname