我必须收集和存储关于我工作的许多事情的统计数据,特别是在一张桌子上我突然遇到了这个过程的问题。获取这些统计信息过程的相关部分只是将数据转储到临时表中,并将该表中的任何新数据插入到主表中。所以我有这样的事情:
CREATE TABLE "TEMP_SCHEMA"."STATS_ORDERS"
(
"ORDER_NO" NUMBER(10,0) NOT NULL ENABLE,
"ORDER_DATE" DATE NOT NULL ENABLE,
"ORDER_TYPE" VARCHAR2(1 BYTE) NOT NULL ENABLE
); /
GRANT SELECT,INSERT,UPDATE,DELETE
ON "TEMP_SCHEMA"."STATS_ORDERS" TO "MY_SCHEMA"; /
CREATE TABLE "MY_SCHEMA"."STATS_ORDERS"
(
"ORDER_NO" NUMBER(10,0) NOT NULL ENABLE,
"ORDER_DATE" DATE NOT NULL ENABLE,
"ORDER_TYPE" VARCHAR2(1 BYTE) NOT NULL ENABLE,
CONSTRAINT "PK_STATS_ORDERS" PRIMARY KEY ("ORDER_NO", "ORDER_TYPE")
); /
将数据转储到“TEMP_SCHEMA”后。“STATS_ORDERS”我正在尝试这样的事情:
INSERT INTO MY_SCHEMA.STATS_ORDERS (ORDER_NO,ORDER_DATE,ORDER_TYPE)
SELECT
DISTINCT TMP.ORDER_NO,TMP.ORDER_DATE,TMP.ORDER_TYPE
FROM
(SELECT DISTINCT * FROM TEMP_SCHEMA.STATS_ORDERS) TMP
LEFT JOIN MY_SCHEMA.STATS_ORDERS ORD
ON TMP.ORDER_NO = ORD.ORDER_NO AND TMP.ORDER_TYPE = ORD.ORDER_TYPE
WHERE ORD.ORDER_NO IS NULL AND ORD.ORDER_TYPE IS NULL; /
当我运行它时,我收到以下错误:
SQL Error: ORA-00001: unique constraint (MY_SCHEMA.PK_STATS_ORDERS) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
我已运行以下查询以查找导致错误的行:
SELECT ORDER_NO,ORDER_DATE,ORDER_TYPE
FROM
MY_SCHEMA.STATS_ORDERS O
INNER JOIN (
SELECT DISTINCT TMP.ORDER_NO,TMP.ORDER_DATE,TMP.ORDER_TYPE
FROM (SELECT DISTINCT * FROM TEMP_SCHEMA.STATS_ORDERS) TMP
LEFT JOIN MY_SCHEMA.STATS_ORDERS ORD
ON TMP.ORDER_NO = ORD.ORDER_NO AND TMP.ORDER_TYPE = ORD.ORDER_TYPE
WHERE ORD.ORDER_NO IS NULL AND ORD.ORDER_TYPE IS NULL
) N
ON O.ORDER_NO = N.ORDER_NO AND O.ORDER_TYPE = N.ORDER_TYPE; /
大概这应该返回导致违规的任何行,但当然它什么也不返回,因为我明确排除了任何可能重复的内容。
我究竟做错了什么?
编辑:我不小心将原始名称留在了我的示例中。在我的示例中将其更改为名称。