作者:宇智臣风 | 来源:互联网 | 2023-09-16 08:33
I have 3 tables such as
我有3张桌子
IndentHeader:
IndentHeader:
IndentID StatusID
-------- ------
1 5
2 5
IndentDetail:
IndentDetail:
IndentID ItemID ItemStatusID
-------- ------ ------------
1 22 4
1 23 4
2 11 4
2 12 3
2 13 3
POIndent:
POIndent:
POID IndentID ItemID ItemStatusID
-------- ------ ------ ------------
1 1 22 4
1 1 23 4
1 2 11 4
I want to Update IndentHeader table StatusID = 4 when all the Items in the IndentDetail table (based on the IndentID) ItemstatusID becomes 4 otherwise I want to Update IndentHeader StatusID = 3. In the Condition I need to give POID. Based on the POID, the corresponding Indent is considered for both the IndentHeader and IndentDetail table. My desired Result should be like this:
当IndentHeader表格中的所有项(基于IndentID)都变成4时,我想更新IndentHeader表格中的小雕像sid = 4,否则我想更新IndentHeader小雕像sid = 3。在这种情况下,我需要给POID。基于POID,对IndentHeader和IndentDetail表都考虑了相应的缩进。我期望的结果应该是这样的:
IndentHeader:
IndentHeader:
IndentID StatusID
-------- ------
1 4
2 3
How to achieve this? Please help me.
如何实现呢?请帮助我。
Hi all, this is my update command. But it update both the StatusID in IndentHeader as 4.
大家好,这是我的更新命令。但是它同时更新了IndentHeader中的小雕像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
I need all your valuable contributions. please help me...
我需要你所有的宝贵贡献。请帮我…
2 个解决方案
3
My [revised] solution use one ALL subquery to check ItemStatusID condition:
我的[修改过的]解决方案使用一个全子查询来检查itemcontrollsid条件:
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;