我有以下表格:
MySQL表: member_interact
CREATE TABLE `member_interact` (
`INT_MEMBER_ID` int(11) NOT NULL,
`INT_ID` int(11) NOT NULL AUTO_INCREMENT,
`INT_SOURCE` varchar(1) NOT NULL,
`INT_DATE` datetime DEFAULT NULL,
`INT_TYPE` varchar(30) NOT NULL,
`COPY_TO_STG` varchar(12) NOT NULL DEFAULT 'NO',
`NEW_STG_SEQ` int(11) DEFAULT NULL,
`COPY_TO_STG_DATE` datetime DEFAULT NULL,
PRIMARY KEY (`INT_ID`),
UNIQUE KEY `INT_ID_UNIQUE` (`INT_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=31246 DEFAULT CHARSET=utf8$$
Oracle表: MEMBER_INTERACT_MYSQL_STG
CREATE TABLE "JTI_HTP"."MEMBER_INTERACT_MYSQL_STG"
( "INT_MEMBER_ID" NUMBER(10,0) NOT NULL ENABLE,
"INT_ID" NUMBER(10,0),
"INT_SOURCE" NVARCHAR2(1) NOT NULL ENABLE,
"INT_DATE" DATE,
"INT_TYPE" NVARCHAR2(30) NOT NULL ENABLE,
"INSERTING_DATE" DATE,
"MYSQL_ID" NUMBER(12,0)
) SEGMENT CREATION DEFERRED
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
TABLESPACE "USERS" ;
基本上,我需要将 member_interact 复制到 member_interact_mysql_stg 并添加新列,如下所示:
我创建了以下程序:
create or replace PROCEDURE COPY_MYSQL_MOB_INT_TO_STG(
P_BATCH_NO IN NUMBER)
IS
CURSOR src
IS
SELECT *
FROM "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP"
WHERE "NEW_STG_SEQ" IS NULL
AND "INT_ID" <= 7000;
STG_INT_ID NUMBER;
BEGIN
FOR des IN src
LOOP
STG_INT_ID := STG_SEQ.NEXTVAL;
INSERT
INTO MEMBER_INTERACT_MYSQL_STG VALUES
(
DES.INT_MEMBER_ID,
STG_INT_ID,
des.int_source,
des.int_date,
des.int_type,
NULL,
DES.INT_ID
);
UPDATE_COPIED_MEMBER_INTERACT(STG_INT_ID, DES.INT_ID);
COMMIT;
END LOOP;
END;
create or replace PROCEDURE UPDATE_COPIED_MEMBER_INTERACT( STG_INT_ID IN NUMBER, MYSQL_INT_ID IN NUMBER)
IS
BEGIN
UPDATE "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP"
SET "COPY_TO_STG" = 'YES',
"NEW_STG_SEQ" = STG_INT_ID
WHERE "INT_ID" = MYSQL_INT_ID;
END;
事实上,有一个程序来更新两个表,但由于我收到的错误,我发现通过将过程分成两个来消除错误可能更容易。 我得到的错误是:
ORA-02047:无法加入正在进行的分布式事务 ORA-06512:at&#34; JTI_HTP.UPDATE_COPIED_MEMBER_INTERACT&#34;,第19行 ORA-06512:at&#34; JTI_HTP.COPY_MYSQL_MOB_INT_TO_STG&#34;,第28行 ORA-06512:第6行
第19行是调用第二个过程的地方。我怀疑这与MySQL表更新有关。
更新
我根据@Maheswaran Ravisankar的答案更新了我的脚本如下:
CREATE OR REPLACE PROCEDURE COPY_MYSQL_MOB_INT_TO_STG_V3(
P_BATCH_NO IN NUMBER)
IS
BEGIN
INSERT INTO MEMBER_INTERACT_MYSQL_STG
SELECT "INT_MEMBER_ID",
STG_SEQ.NEXTVAL,
"INT_SOURCE",
"INT_DATE",
"INT_TYPE",
CURRENT_DATE,
"INT_ID"
FROM "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP" des
WHERE "NEW_STG_SEQ" IS NULL;
UPDATE "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP" A
SET "COPY_TO_STG" = 'YES',
"NEW_STG_SEQ" =
(SELECT STG_INT_ID
FROM MEMBER_INTERACT_MYSQL_STG B
WHERE A."INT_ID" = B.STG_INT_ID
);
END;
但是,我收到错误 ORA-02070:数据库JTIPARTNER_HTP在此上下文中不支持子查询。
答案 0 :(得分:1)
INSERT
INTO MEMBER_INTERACT_MYSQL_STG
SELECt
DES.INT_MEMBER_ID,
STG_SEQ.NEXTVAL,
des.int_source,
des.int_date,
des.int_type,
NULL,
DES.INT_ID
FROM "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP" des
WHERE "NEW_STG_SEQ" IS NULL
/* RETURNING STG_INT_ID BULK COLLECT INTO SOME RECORD; --Needed if only processed in array!*/
并在下面更新,
UPDATE "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP" A
SET "COPY_TO_STG" = 'YES',
"NEW_STG_SEQ" = (SELECT STG_INT_ID FROM MEMBER_INTERACT_MYSQL_STG B
WHERE A.INT_ID = B.INT_ID);
(OR)
FOR REC IN (SELECT STG_INT_ID,INT_ID FROM MEMBER_INTERACT_MYSQL_STG)
LOOP
UPDATE "jtipartn_mydb"."MEMBER_INTERACT"@"JTIPARTNER_HTP" A
SET "COPY_TO_STG" = 'YES',
"NEW_STG_SEQ" = REC.STG_INT_ID
WHERE A.INT_ID = REC.INT_ID;
END LOOP;