2

ストアド プロシージャ内に次のスニペットがあります。

(注: これは元のスニペットではありませんが、テーブルと列の名前がより一般的なものに置き換えられていることを除いて、元のスニペットとまったく同じです。理解を深め、クライアント コードの配布を防ぎます。)

UPDATE table_Orders c SET order_type = 
(
SELECT 'PE' FROM 
table_Orders A, 
table_Orders b, 
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 
AND cust_category = 'PREMIUM' ) d 
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id 
AND A.shipment_type = c.shipment_type AND  A.order_availability = c.order_availability AND A.product_id = c.product_id 
);
COMMIT;

さて、私の問題はORA-01427 single-row subquery returns more than one row、手順の実行中にエラーが発生することです。以下のように句を使用してステートメントの最後にサブクエリを配置しようとしましたINが、それでも機能しませんでした。

UPDATE table_Orders c SET order_type =
(
SELECT 'PE' FROM 
table_Orders A, 
table_Orders b
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id 
AND A.shipment_type = c.shipment_type AND  A.order_availability = c.order_availability AND A.product_id = c.product_id 
AND (A.order_id, A.cust_id, A.order_date) IN
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders 
    WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 AND cust_category = 'PREMIUM')
);
COMMIT;

誰かが私を正しい方向に案内してくれませんか?

JOIN を使用したコード --> これにより、「JOIN」ステートメントの直前の c.product_Id に右括弧がありません

UPDATE table_Orders c SET order_type =
(
 SELECT 'PE' FROM 
table_Orders A, 
table_Orders b
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id 
AND A.shipment_type = c.shipment_type AND  A.order_availability = c.order_availability AND A.product_id = c.product_id 
JOIN
SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders 
WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 AND cust_category = 'PREMIUM' d
ON A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
COMMIT;
4

1 に答える 1

0

サブクエリはおそらくいくつかの行を複数返していますc.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id

テーブルの構造とデータがわからないと、わかりにくいです。

しかし、あなたはこのようなことをすることができます:

UPDATE table_Orders c SET order_type = 'PE'
WHERE (c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id) IN 
(
SELECT A.order_id, A.order_date, A.cust_id, A.shipment_type, A.order_availability, A.product_id 
FROM 
table_Orders A, 
table_Orders b, 
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 
AND cust_category = 'PREMIUM' ) d 
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
);

COMMIT;

ところで、「JOIN構文」を使ってみませんか?


UPDATE同じソリューションですが、ANSI結合構文を使用します。

UPDATE table_Orders c 
   SET order_type = 'PE'
 WHERE (c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id) IN 
(
SELECT A.order_id, A.order_date, A.cust_id, A.shipment_type, A.order_availability, A.product_id 
FROM 
table_Orders A 
JOIN 
table_Orders b ON A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id 
JOIN 
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 
AND cust_category = 'PREMIUM' ) d ON A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
WHERE ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
);
于 2013-03-19T05:22:33.957 に答える