给你个事例,将满足name='jz'的记录删除
CREATE TRIGGER [test_trigger] ON [dbo].[test] 
FOR DELETE 
AS
begin
 set  XACT_ABORT on
declare @sn_del varchar(20),@sn_ins varchar(20),@cun as  int,@temp  varchar(20)
set @sn_del=''
set @sn_ins=''select @sn_del=id from deleted del where name='jz'
select @sn_ins=id from inserted ins where name='jz'if (@sn_del='' and   @sn_ins=''   )
begin
 set  @temp=''
endelse
begin    
    select @cun=count(*) from test where id=@sn_ins
     if @cun>0 
     begin
         delete test where operatorid=@sn_ins
    
     end
    else
    begin
         delete test where operatorid=@sn_del
    endend endGO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

解决方案 »

  1.   

    我说的好像不是这个意思
    我重新表达一下id    name    num
    1     ukyo    32
    2     xh      43
    3     ux      52
    我想在删除某条记录的时候进行验证,如果num>40就可以删除,小于就不能删除
      

  2.   

    一样的啊,你把判断条件变为num>40就好了啊.
    select @sn_del=id from deleted del where  num>40
    select @sn_ins=id from inserted ins where num>40
      

  3.   

    CREATE TRIGGER [test_trigger] ON [dbo].[test] 
    FOR DELETE 
    AS
    begin
     set  XACT_ABORT on
    declare @sn_del varchar(20),@sn_ins varchar(20),@cun as  int,@temp  varchar(20)
    set @sn_del=''
    set @sn_ins=''select @sn_del=id from deleted del where  num>40
    select @sn_ins=id from inserted ins where num>40if (@sn_del='' and   @sn_ins=''   )
    begin
     set  @temp=''
    endelse
    begin    
        select @cun=count(*) from test where id=@sn_ins
         if @cun>0 
         begin
             delete test where operatorid=@sn_ins
        
         end
        end 
      

  4.   

    用替代触发器CREATE TRIGGER [test_trigger] ON [dbo].[test] 
    INSTEAD OF  DELETE 
    as
    ...
      

  5.   

    楼主,上面的一段改一下。再试试:
    begin    
        select @cun=count(*) from test where id=@sn_del
         if @cun>0 
         begin
             delete test where operatorid=@sn_del    
         end
        end
      

  6.   

    不好意思,不是operatorid ,应该是你自己的号
    begin    
        select @cun=count(*) from test where id=@sn_del
         if @cun>0 
         begin
             delete test where id=@sn_del    
         end    end
      

  7.   

    --删除触发器例子:
    create table t(id int,name varchar(10),num int)
    insert into t(id,name,num)
    select 1,     'ukyo',    32 union all
    select 2,     'xh',      43 union all
    select 3,     'ux',      52
    gocreate trigger [tr_delete] on t
    for delete
    AS
    if exists(select * from deleted where num<=40)
    begin
      rollback transaction
      raiserror('num<=40,不允许删除!!!',16,1)
    end
      

  8.   

    AFTER 触发器在触发操作(INSERT、UPDATE 或 DELETE)后和处理完任何约束后激发。可通过指定 AFTER 或 FOR 关键字来请求 AFTER 触发器。因为 FOR 关键字与 AFTER 的效果相同,所以具有 FOR 关键字的触发器也归类为 AFTER 触发器。
    INSTEAD OF 触发器代替触发动作进行激发,并在处理约束之前激发。 有个疑问,既然是要验证是否满足条件才觉得是否删除,那为什么不直接用select?如果真要用触发器,那不是要建instead of trigger,然后在判断是否要删除,不是多此一举了?菜鸟说的话要是错了请见谅!