四个触发器:它不起作用

时间:2015-04-13 11:36:46

标签: triggers subquery

enter image description here

各位大家好, 请原谅我的英语不好 我试图解决我的问题超过4天了: 每个触发器都运行良好但是当我将它们组合起来时会出现错误: the subquery returns more than 1 value

我试图遵循本网站和其他人的所有提示,但我无法使其有效。 有关表格包括:PIECES,COMPOSITIONSGAMMES,术语和情况。 我希望触发器做的是:

  1. 当用户在" SITUATIONS"上插入新行时如果' nomstrategie' =" DST" (它是一个策略的名称,但这个细节并不重要,我的意思是对于那些会帮助我的人),我需要插入其他行以相同的参考(参考),相同的策略(nomstrategie)。只有' ancienposte'和' nouveauposte'必须改变。事实上,第一个价值必须是全部的Numeroposte'从表格" Compositionsgammes"。第二个值必须是' ???'

  2. 我需要,当我插入一个新行并且' nomstrategie' =' DST'时,其他行将插入所有' piecesfilles'在表格" Nomenclatures" 参考文献'参考文献'在用户插入的行中。而且在“ancienposte'”中,应该有“numeroposte'在表格" compositiongammes"。

  3. 我需要,当用户插入新行并且“删除”时,要插入另一行,例如:

    插入行:参考A ancienposte:P01 Nouveauposte:P02 Nomstrategie:Delestage ............

    要插入的行:Ref A ancienposte:P02 Nouveauposte:NULL Nomstrategie:Delestage ............

  4. 我需要,对于表格中的每一行"情境",计算一个名为' charge'在表中情况charge =(TS / Taillelot)+ Tu

  5. 以下是我已经完成的触发器:

    create trigger [dbo].[ALLDST]
    ON [dbo].[SITUATIONS]
    AFTER INSERT /*pas d'update*/
    as 
            begin
            set nocount on
                insert into SITUATIONS(ReferencePiece,nomstrategie,AncienPoste,nouveauposte,DateStrategie)
                select distinct i.referencepiece, i.nomstrategie,COMPOSITIONSGAMMES.NumeroPoste,'???',i.DateStrategie
    
                from inserted i, PIECES, compositionsgammes, SITUATIONS s
                where i.ReferencePiece is not null 
                and i.NomStrategie='DST' 
                and i.ReferencePiece=pieces.ReferencePiece and pieces.CodeGamme=COMPOSITIONSGAMMES.CodeGamme 
                and i.AncienPoste<>COMPOSITIONSGAMMES.NumeroPoste
                and i.DateStrategie=s.DateStrategie
    
                end
    
    
    
    create trigger [dbo].[Calcul_Charge]
    on [charges].[dbo].[SITUATIONS]
    after insert 
    as
    begin
    
    
    update situations
    
    set charge= (select (cg.TS/pieces.TailleLot)+cg.tu from situations s
    inner join COMPOSITIONSGAMMES cg on cg.NumeroPoste=SITUATIONS.AncienPoste
    inner join pieces on SITUATIONS.ReferencePiece=pieces.ReferencePiece 
    inner join inserted i on s.DateStrategie=i.DateStrategie
    where cg.CodeGamme=pieces.CodeGamme and NumeroPoste=situations.AncienPoste
    )   
    
    end
    
    
    create trigger [dbo].[Duplicate_SITUATIONS]
    ON [dbo].[SITUATIONS]
    AFTER INSERT
    as 
            begin
            set nocount on
            declare @ref varchar(50)
            declare @strategie varchar(50)
            declare @ancienposte varchar(50)
            declare @datestrategie date
            declare @pourcentage decimal(18,3)
            declare @coeff decimal(18,3)
            declare @charge decimal(18,3)
            /*while (select referencepiece from situations where ReferencePiece)  is not null*/
                select @ref=referencepiece, @strategie=nomstrategie,@ancienposte=NouveauPoste,
                @datestrategie=datestrategie, @pourcentage=PourcentageStrategie,@coeff=coeffameliorationposte,@charge=charge
                from inserted,POSTESDECHARGE 
                where ReferencePiece is not null
                and POSTESDECHARGE.NumeroPoste = inserted.AncienPoste
                if @strategie = 'delestage' and @ancienposte is not null
                /*if GETDATE()>= (select datestrategie from SITUATIONS)*/
                begin
                insert into SITUATIONS(ReferencePiece, nomstrategie,AncienPoste,DateStrategie,
                StatutStrategie,DateModification,PourcentageStrategie,charge)
                values
                (@ref, @strategie, @ancienposte, @datestrategie,1,getdate(),@pourcentage,@charge*@coeff)
                end
                end
    

1 个答案:

答案 0 :(得分:0)

我最熟悉T-SQL(MS SQL),不确定这是否适用于您的情况..但我通常使用子查询避免更新并重写您的更新:

update situations

set charge= (select (cg.TS/pieces.TailleLot)+cg.tu from situations s
inner join COMPOSITIONSGAMMES cg on cg.NumeroPoste=SITUATIONS.AncienPoste
inner join pieces on SITUATIONS.ReferencePiece=pieces.ReferencePiece 
inner join inserted i on s.DateStrategie=i.DateStrategie
where cg.CodeGamme=pieces.CodeGamme and NumeroPoste=situations.AncienPoste
)   

如下

update s set 
    charge= (cg.TS/pieces.TailleLot)+cg.tu
from situations s
    inner join COMPOSITIONSGAMMES cg on cg.NumeroPoste=SITUATIONS.AncienPoste
    inner join pieces on SITUATIONS.ReferencePiece=pieces.ReferencePiece 
    inner join inserted i on s.DateStrategie=i.DateStrategie
where cg.CodeGamme=pieces.CodeGamme and NumeroPoste=situations.AncienPoste