四个触发器:它不起作用

Four triggers: It does not work

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

我尝试遵循本网站和其他网站中的所有提示,但我无法使其正常工作。 相关的 table 是:PIECES、COMPOSITIONSGAMMES、命名法和 SITUATIONS。 我想让触发器做的是:

  1. 当用户在 "SITUATIONS" 上插入一个新行并且如果 'nomstrategie'= "DST" (这是策略的名称,但这个细节并不重要,我的意思是那些会帮助我的人),我需要插入具有相同参考(referencepiece)和相同策略(nomstrategie)的其他行。只有 'ancienposte' 和 'nouveauposte' 必须更改。实际上,第一个的值必须是 table "Compositionsgammes" 中的所有 'Numeroposte'。第二个的值必须是 '???'.

  2. 我需要,当我插入一个新行并且'nomstrategie'='DST',其他行被插入所有'piecesfilles'在table "Nomenclatures" 用户插入的行中的引用 'referencepiece'。而在'ancienposte'中,table"compositionsgammes"中应该有'numeroposte'。

  3. 我需要,当用户插入一个新行并且'nomstrategie'='delestage,另一行被插入如下,例如:

    插入的行:Ref A ancienposte:P01 Nouveauposte:P02 Nomstrategie:Delestage…………

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

  4. 我需要,对于 table "situations" 中的每一行,在 table 情况下计算一个名为 'charge' 的值 charge=(TS/Taillelot)+图

这是我完成的触发器:

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

我最熟悉 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