USE [SalesManagementSystem]
GO
/****** Object:  Trigger [dbo].[Trigger_FDistrictID]    Script Date: 06/25/2010 19:04:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOCREATE Trigger [dbo].[Trigger_FDistrictID]
       On [dbo].[T_BasicInformation_District]                       
     FOR    DELETE                           
     As                                        
     BEGIN
       
         
           DECLARE @FDistrictID VARCHAR(20) 
         SELECT FDistrictID = @FDistrictID  FROM DELETED 
         DELETE  FROM T_BasicInformation_Customer 
         WHERE @FDistrictID =FDistrictID
 
                 
     END  
以上是触发器   
现在执行
DELETE  FROM T_BasicInformation_Customer  where FDistrictID = 4
SELECT * FROM T_BasicInformation_District
SELECT * FROM T_BasicInformation_Customer
后 发现个问题:
DELETE  FROM T_BasicInformation_Customer  where FDistrictID = 4  这句 没有起到作用
触发器执行却没问题  为什么?

解决方案 »

  1.   

    FDistrictID 是你查出來的字段名麼?
      

  2.   

     DECLARE @FDistrictID VARCHAR(20) 
             DELETE  FROM T_BasicInformation_Customer 
             WHERE FDistrictID =@FDistrictID直接這樣不要查詢了。。
      

  3.   

    楼主,你写倒了
             SELECT FDistrictID = @FDistrictID  FROM DELETED 
    应该是
     SELECT @FDistrictID  = FDistrictID  FROM DELETED 同样下面一句也倒了
             DELETE  FROM T_BasicInformation_Customer 
             WHERE @FDistrictID =FDistrictID
      

  4.   

    SELECT @FDistrictID  =FDistrictID  FROM DELETED 
      

  5.   

    ALTER Trigger [dbo].[Trigger_FDistrictID]
           On [dbo].[T_BasicInformation_District]                       
         for    DELETE                           
         As                                        
         BEGIN
           
             
               DECLARE @FDistrictID VARCHAR(20) 
             SELECT @FDistrictID = FDistrictID  FROM DELETED 
             DELETE  FROM T_BasicInformation_Customer 
             WHERE FDistrictID = @FDistrictID
         END     
    2个结果一样
      

  6.   

    create trigger t1
    on topcinfo
    instead of delete
    as
    delete replayinfo
    where id in (select topcid from deleted)
    delete from topcinfo
    where topcid in (select topcid in deleted)
      

  7.   

    这类问题请到SQL Server论坛去问比较好。不过我要说的是,如果你不是“精通”Oracle的,那么你写的这个所谓触发器是不可原谅的有严重BUG的。SQL Server与Oracle的触发器的不同,在于后者是每一行上都会执行触发器,而前者是在整个一个SQL命令上仅仅执行一次触发器。因此,DELETED 集合中完全可能有不止一条记录,甚至可能有几千条记录。既然写一条代码都出严重的BUG,有何必抱着侥幸呢?