更新多行的语句

时间:2015-05-05 21:24:38

标签: sql-server sql-server-2012 sql-update

我对更新声明感到担忧。 我的表的主键由以下列组成: ReferencePiece,NumeroPhase,AncienPoste,DateStrategie,DateCadence,NomProgramme,VersionProgramme。 这是我的程序:

UPDATE S
SET S.stock = T.Stock - T.NombrePiecesParAvion * T.CadenceProgramme
FROM SITUATIONS_Final_1 S
INNER JOIN SITUATIONS_Final_1 T ON S.ReferencePiece = T.ReferencePiece
    AND S.NumeroPhase = T.NumeroPhase
    AND S.AncienPoste = T.AncienPoste
    AND T.DateDebProduction < S.DateDebProduction
    AND T.stock = (
        SELECT min(T.stock)
        FROM SITUATIONS_Final_1 T
        )
WHERE S.DateDebProduction = (
        SELECT min(S.datedebproduction) AS 'date1'
        FROM SITUATIONS_Final_1 S
        INNER JOIN SITUATIONS_Final_1 T ON S.ReferencePiece = T.ReferencePiece
            AND S.NumeroPhase = T.NumeroPhase
            AND S.AncienPoste = T.AncienPoste
        WHERE S.datedebproduction > T.DateDebProduction
            AND T.stock = (
                SELECT min(T.stock)
                FROM SITUATIONS_Final_1 T
                )
        )

但实际上,由于我的更新声明中没有“分组依据”,它只考虑我所有引用的最小值(库存)并对未来日期进行更新。

我尝试按照以下方式进行,但我确信它比这更容易:

UPDATE S
SET S.stock = T.Stock - T.NombrePiecesParAvion * T.CadenceProgramme
FROM SITUATIONS_Final_1 S
INNER JOIN SITUATIONS_Final_1 T ON S.ReferencePiece = T.ReferencePiece
    AND S.NumeroPhase = T.NumeroPhase
    AND S.AncienPoste = T.AncienPoste
    AND T.DateDebProduction < S.DateDebProduction
INNER JOIN (
    SELECT referencepiece
        ,NumeroPhase
        ,AncienPoste
        ,DateStrategie
        ,nomprogramme
        ,versionprogramme
        ,datedebproduction
        ,min(stock) StockMini
    FROM SITUATIONS_Final_1
    GROUP BY ReferencePiece
        ,NumeroPhase
        ,AncienPoste
        ,DateStrategie
        ,nomprogramme
        ,versionprogramme
        ,DateDebProduction
    ) F ON F.ReferencePiece = T.ReferencePiece
    AND T.Stock = F.StockMini
INNER JOIN (
    SELECT referencepiece
        ,NumeroPhase
        ,AncienPoste
        ,DateStrategie
        ,nomprogramme
        ,versionprogramme
        ,min(datedebproduction) datemini
        ,stock
    FROM SITUATIONS_Final_1
    GROUP BY ReferencePiece
        ,NumeroPhase
        ,AncienPoste
        ,DateStrategie
        ,nomprogramme
        ,versionprogramme
        ,DateDebProduction
        ,stock
    ) Z ON F.ReferencePiece = Z.ReferencePiece
WHERE T.DateDebProduction = Z.datemini
    AND Z.datemini = F.DateDebProduction
    AND Z.Stock = F.StockMini
    AND T.ReferencePiece = Z.ReferencePiece
    AND S.datedebproduction > T.DateDebProduction
    AND T.stock = F.StockMini
    AND S.ReferencePiece = T.ReferencePiece
    AND T.ReferencePiece = F.ReferencePiece
    AND F.numerophase = Z.numerophase
    AND T.numerophase = Z.numerophase
    AND F.AncienPoste = Z.AncienPoste
    AND T.AncienPoste = Z.AncienPoste
    AND F.DateStrategie = Z.DateStrategie
    AND T.DateStrategie = Z.DateStrategie
    AND F.nomprogramme = Z.nomprogramme
    AND T.nomprogramme = Z.nomprogramme
    AND F.versionprogramme = Z.versionprogramme
    AND T.versionprogramme = Z.versionprogramme
    AND S.ReferencePiece = T.ReferencePiece
    AND S.NumeroPhase = T.NumeroPhase
    AND S.AncienPoste = T.AncienPoste
    AND S.nomprogramme = T.nomprogramme
    AND S.versionprogramme = T.versionprogramme
    AND S.DateStrategie = T.datestrategie

但它表现不佳:它更新相同的行(相同的日期)。我需要这个程序来做这样的事情: Referencepiece = RefA DateDebProduction = '02 / 05/2015'股票= 4 NbrePieces = 2 CadenceProgramme = 1 对于相同的参考和下一个日期股票应该是= 4-2 * 1 = 2 ...  实际上,这个程序包含在另一个程序中。它启动它5次,以便我可以确定它将更新我的所有行。如果你可以帮我找到一个更好的方法来更新所有行而不需要多次启动我的程序,那就太棒了。 非常感谢你

1 个答案:

答案 0 :(得分:0)

以下是数据样本:

ReferencePiece NumeroPhase AncienPoste DateStrategie Charge NomProgramme VersionProgramme CadenceProgramme DateCadence DateDebProduction NombrePiecesParAvion Stock

RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 29.000 01/03/2015 01/03/2015 1.000 23.000 RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 25.000 01/04/2015 01/04/2015 1.000 NULL RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 19.000 01/05/2015 01/05/2015 1.000 NULL RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 25.000 01/06/2015 01/06/2015 1.000 NULL RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/05/2015 01/05/2015 2.000 12.000
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/06/2015 01/06/2015 2.000 8.000
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/07/2015 01/07/2015 2.000 NULL
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 3.000 01/08/2015 01/08/2015 2.000 NULL