我就是插了代码后,出问题了
我在StockRecord表插入了数据后,触发器起作用,Storage表的Amount增加了;接着在Bank表插入了数据后,触发器起作用,User表的Cash增加;但是我接着在SellRecord表中插入数据,触发器就不起作用了;
好像是只能用一个一样,我要是先在SellRecord表中插入数据,User表的Cash减少,Storage表的Amount减少; 要是再接着向StockRecord表或Bank表插入数据,触发器就没用了;反过来接着不管向那个表插数据都触发器都不起作用了。反过来插也一样

解决方案 »

  1.   

    select @Gastype = Gastype from inserted
    select @Depot = Depot from inserted
    select @Amount = Amount from inserted
    select @Pname = Pname from inserted樓主這种語句可以這樣寫:select @Gastype = Gastype ,@Depot = Depot ,@Amount = Amount ,@Pname = Pname from inserted  另外你可以在查詢分析器這樣調試begin tran 
      insert into ......(這里寫測試語句)
      select * from tab  這里寫查詢語句檢查觸發器是否有用
    rollback tran   ---因執行完後回滾,就算程序出錯也不會留下拉圾,可以修正後重新運行,非常方便然後在要測試的觸發器中用Print 或select 語句檢查是否最入了觸發器,
      

  2.   

    create  Trigger AutoProcessSell
    On SellRecord
    After Insert
    AsDeclare @Gastype as  nvarchar(50)
    Declare @Depot as    nvarchar(50)
    Declare @Amount as float
    Declare @Pname  as    nvarchar(50)
    Declare @cur_Amount as float
    Declare @Cash as float
    Declare @Price as float
    declare @count intselect @Gastype = Gastype from inserted
    select @Depot = Depot from inserted
    select @Amount = Amount from inserted
    select @Pname = Pname from inserted
    select @price = price from inserted
    --select @Price = Price from GasType where type=@Gastype   是不是还有一个Gastype表来存放PRICE,暂时不要了。 
    --update SellRecord set Price=@Price where Gastype=@Gastypeselect @count=(select count(*) from storage where gastype=@gastype and depot=@depot)
    if @count='0'
    begin
    insert into storage (gastype,depot,amount) values(@gastype,@depot,@amount)
    end
    else
    begin
    select @cur_Amount = Amount from Storage where Gastype = @Gastype and Depot = @Depot
    Update Storage set Amount = (@cur_Amount-@Amount) where Gastype = @Gastype and Depot = @Depot
    end
    /*
    Select @Cash = Cash from Users where Pname = @Pname
    Update Users set Cash = (@Cash-@Amount*@Price) where Pname = @Pname*/
      

  3.   

    你试下这个,但是这个触发时你的USERS表不会有变化的,因为我知道你那个表到底想要做什么
      

  4.   

    有个问题就是触发器是不是能不能在一次调试中连续触发两个 他们改变的是同一个变量 
    比如说我现触发了个触发器A ,给变量SD+5 ; 再触发触发器B ,给变量SD-5
      

  5.   

    我的意思是两个触发器操作一个表的同一数据
    就像  Trigger AutoProcessSell和Trigger AutoInsertStorage操作表Storage的Amount 一样 
    一个减,一个加
      

  6.   

    经验证  Trigger AutoInsertBank和Trigger AutoProcessSell没问题 可以通过Bank 表触发 AutoInsertBank修改Users 表的Cash 使Cash 增加 ;也可以通过SellRecord表触发AutoProcessSell修改Users 表的Cash 使Cash 减少
    问题就在
    Trigger AutoProcessSell和Trigger AutoInsertStorage  能不能 通过SellRecord 表触发修改Storage 表的Amount 使Amount减少 ;通过SellRecord表触发修改Storage  表的Amount使Amount减少我实在搞不清到底是什么问题 ,望大家帮帮忙 ,给我指出下  小弟在这谢谢大家了 
      

  7.   

    问题::Trigger AutoProcessSell和Trigger AutoInsertStorage  能不能 通过SellRecord 表触发修改Storage 表的Amount 使Amount减少 ;接着再通过StockRecord表触发修改Storage  表的Amount使Amount增加上面的写错了 不好意思 主要是头大了
      

  8.   

    当然可以啦,你的STOCKRECORD表的触发器不是可以起作用的吗?
      

  9.   

    TO:unkmass(拉克) 触发器可以用就说明你这个触发器没问题,
    就不再再问A表的触发器可以触发,但B表的触发器不可以触发,
    既然你已经可以判断出B表的触发器不可以触发,你就重点在B触发器上找答案了,
    另外,我给你写的那个触发器已经可以帮你解决“通过SellRecord 表触发修改Storage 表的Amount 使Amount减少 ”你这个问题,已经帮写你写的够清楚了。