Triggers 四个触发因素:它不起作用

Triggers 四个触发因素:它不起作用,triggers,subquery,Triggers,Subquery,大家好,, 请原谅我英语不好 4天多以来,我一直在努力解决我的问题: 每个触发器都工作正常,但当我将它们组合在一起时,会出现错误: 子查询返回多个值 我试图遵循这个网站和其他网站上的所有提示,但我无法让它工作。 相关表格包括:片段、合成片段、术语和情况。 我想让触发器做的是: 当用户在情境中插入新行时,如果“nomstrategie”=DST,这是一个策略的名称,但这个细节实际上并不重要,我的意思是,对于帮助我的人来说,我需要插入具有相同引用引用的其他行,相同的strategynomstrate

大家好,, 请原谅我英语不好 4天多以来,我一直在努力解决我的问题: 每个触发器都工作正常,但当我将它们组合在一起时,会出现错误: 子查询返回多个值

我试图遵循这个网站和其他网站上的所有提示,但我无法让它工作。 相关表格包括:片段、合成片段、术语和情况。 我想让触发器做的是:

当用户在情境中插入新行时,如果“nomstrategie”=DST,这是一个策略的名称,但这个细节实际上并不重要,我的意思是,对于帮助我的人来说,我需要插入具有相同引用引用的其他行,相同的strategynomstrategie。只有“古代邮政”和“新邮政”需要改变。事实上,第一个值必须是表CompositionGames中的所有“Numeroposte”。第二个值必须是“??”

我需要,当我插入一个新行并且'nomstrategie'='DST'时,插入表命名中所有'piecesfilles'的其他行 用户插入的行中引用“referencepice”的。在“ancienposte”中,表CompositionGammes中应该有“numeroposte”

我需要,当用户插入新行且'nomstrategie'='delestage'时,插入另一行,如下所示,例如:

插入行:参考A古代邮政:P01新邮政:P02 Nomveauposte:P02 Nomstrategie:Deletage

要插入的行:参考A辅助Poste:P02 Nouveauposte:NULL Nomstrategie:Delestage

我需要,对于表格情境中的每一行,在表格情境charge=TS/Taillelot+Tu中计算一个名为“charge”的值

以下是我所做的触发:

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

谢谢你的回答。它起作用了,但没有完全解决问题。我有个问题:你的要求和我的很相似。他们之间的真正区别是什么?谢谢,我没有使用子查询来获取字段的值,如果有多行符合条件,这将更新多行。