基于3个表更新列?

时间:2011-09-05 05:37:40

标签: sql sql-server sql-server-2005 join sql-update

我有3个表,例如

IndentHeader:

 IndentID        StatusID 
--------         ------   
    1             5      
    2             5

IndentDetail:

IndentID         ItemID           ItemStatusID
--------         ------           ------------
 1                22                 4
 1                23                 4
 2                11                 4
 2                12                 3
 2                13                 3

POIndent:

       POID             IndentID     ItemID      ItemStatusID
      --------           ------      ------      ------------
         1                1            22            4
         1                1            23            4
         1                2            11            4

我想更新IndentHeader表StatusID = 4当IndentDetail表中的所有项目(基于IndentID)ItemstatusID变为4否则我想更新IndentHeader StatusID = 3.在条件中我需要给出POID。根据POID,IndentHeader和IndentDetail表都考虑相应的Indent。我想要的结果应该是这样的:

IndentHeader:

 IndentID        StatusID 
--------         ------   
    1             4      
    2             3

如何实现这一目标?请帮帮我。

大家好,这是我的更新命令。但它将IndentHeader中的StatusID更新为4。

   UPDATE STR_IndentHeader
            SET StatusID = IID
            FROM
            (SELECT 
            STR_IndentDetail.IndentID, MIN(ItemStatusID) AS 'IID'
            FROM 
            STR_IndentDetail INNER JOIN PUR_POIndent PP
            ON PP.IndentID = STR_IndentDetail.IndentID
            AND PP.ItemID = STR_IndentDetail.ItemID
                        WHERE ItemStatusID = 4 AND PP.POID = 1
            GROUP BY STR_IndentDetail.IndentID) ID 
            WHERE ID.IndentID = STR_IndentHeader.IndentID 

我需要你所有的宝贵贡献。请帮帮我......

2 个答案:

答案 0 :(得分:3)

我的 [modified] 解决方案使用一个ALL子查询来检查ItemStatusID条件:

DECLARE @MyPOID INT = 1;

DECLARE @IndentHeader TABLE
(
    IndentID INT PRIMARY KEY
    ,StatusID INT NOT NULL
);
INSERT  @IndentHeader 
VALUES  (1,5);
INSERT  @IndentHeader 
VALUES  (2,5);
INSERT  @IndentHeader 
VALUES  (3,5);

DECLARE @IndentDetail TABLE
(
    IndentID INT NOT NULL
    ,ItemID INT NOT NULL
    ,ItemStatusID INT NOT NULL
    ,PRIMARY KEY(IndentID, ItemID)
);
INSERT  @IndentDetail
VALUES  (1,22,4);
INSERT  @IndentDetail
VALUES  (1,23,4);
INSERT  @IndentDetail
VALUES  (2,11,4);
INSERT  @IndentDetail
VALUES  (2,12,3);
INSERT  @IndentDetail
VALUES  (2,13,3);
INSERT  @IndentDetail
VALUES  (3,22,3);

DECLARE @POIndent TABLE
(
    POID INT
    ,IndentID INT NOT NULL
    ,ItemID INT NOT NULL
    ,ItemStatusID INT NOT NULL
);
INSERT  @POIndent 
VALUES  (1,1,22,4);
INSERT  @POIndent 
VALUES  (1,1,23,4);
INSERT  @POIndent 
VALUES  (1,2,11,4);
INSERT  @POIndent 
VALUES  (2,3,22,4);

SELECT  *
FROM    @IndentHeader h;
SELECT  *
FROM    @IndentDetail d;
SELECT  *
FROM    @POIndent po;

UPDATE  @IndentHeader 
SET     StatusID = CASE WHEN 4 = ALL(SELECT d.ItemStatusID FROM @IndentDetail d WHERE d.IndentID = h.IndentID) THEN 4 ELSE 3 END
FROM    @IndentHeader h
WHERE   h.IndentID IN (SELECT po.IndentID FROM @POIndent po WHERE po.POID = @MyPOID);

SELECT  *
FROM    @IndentHeader h;

答案 1 :(得分:2)

它的要点是

  • 找到每个ItemStatusID
  • 的最低IndentID
  • 请与IndentHeader
  • 联系
  • UPDATE FROM声明
  • 中使用这些内容

SQL语句

UPDATE  IndentHeader
SET     StatusID = ihd.ItemStatusID
FROM    IndentHeader ih
        INNER JOIN (
            SELECT  ItemStatusID = MIN(id.ItemStatusID)
                    , ih.IndentID
            FROM    IndentHeader ih
                    INNER JOIN IndentDetail id ON id.IndentID = ih.IndentID
                    INNER JOIN POIndent pi ON pi.IndentID = id.IndentID
            WHERE   pi.POID = 1     
            GROUP BY
                    ih.IndentID     
        ) ihd ON ihd.IndentID = ih.IndentID         

测试脚本

;WITH IndentHeader (IndentID, StatusID) AS (
    SELECT 1, 5      
    UNION ALL SELECT 2, 5
)
, IndentDetail (IndentID, ItemID, ItemStatusID) AS (
    SELECT 1, 22, 4
    UNION ALL SELECT 1, 23, 4
    UNION ALL SELECT 2, 11, 4
    UNION ALL SELECT 2, 12, 3
    UNION ALL SELECT 2, 13, 3
)
, POIndent (POID, IndentID, ItemID, ItemStatusID) AS (
    SELECT 1, 1, 22, 4
    UNION ALL SELECT 1, 1, 23, 4
    UNION ALL SELECT 1, 2, 11, 4
)
--UPDATE    IndentHeader
--SET       StatusID = ihd.ItemStatusID
SELECT  ih.IndentID, ihd.ItemStatusID
FROM    IndentHeader ih
        INNER JOIN (
            SELECT  ItemStatusID = MIN(id.ItemStatusID)
                    , ih.IndentID
            FROM    IndentHeader ih
                    INNER JOIN IndentDetail id ON id.IndentID = ih.IndentID
                    INNER JOIN POIndent pi ON pi.IndentID = id.IndentID
            WHERE   pi.POID = 1     
            GROUP BY
                    ih.IndentID     
        ) ihd ON ihd.IndentID = ih.IndentID