简体   繁体   English

在过程中使用表别名的ORA-01427

[英]ORA-01427 with table alias used in Procedure

I have the following snippet within a stored procedure: 我在存储过程中有以下代码片段:

(Note, this is not the original snippet, but exactly similar to it except that table and column names have been replaced with more generic ones for better understanding as well as to prevent giving out client code.) (请注意,这不是原始代码段,但与之完全相似,只是表和列名已替换为更通用的名称,以便于更好地理解并防止泄露客户端代码。)

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;

Now, my issue is that i am getting the error ORA-01427 single-row subquery returns more than one row while executing the procedure. 现在,我的问题是我在执行过程时遇到错误ORA-01427 single-row subquery returns more than one row的错误。 I tried putting the subquery at the end of the statement using an IN clause as below, but even that did not work: 我尝试使用如下的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;

Could someone kindly guide me in the right direction, as to where I'm bungling this up, or how the subquery is already messed up ? 有人可以在正确的方向上引导我,例如我在哪里弄乱它,或者子查询已经被弄乱了吗?

Code With JOIN --> This gives missing right parenthesis at c.product_Id just before the "JOIN" statement 带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;

The subquery is probably returning more than one row for some c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id 对于某些c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id子查询可能返回多个行

Without knowing your table structure and data it's hard for us to tell. 在不知道您的表结构和数据的情况下,我们很难分辨。

But you could do something like this: 但是您可以执行以下操作:

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;

BTW, why don't you use the "JOIN Syntax" ? 顺便说一句,为什么不使用“ JOIN语法”?


UPDATE The same solution but with ANSI join syntax: 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))
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM