如何在oracle中保存最后一次捕获数据以进行数据提取

How to save the last captured date for data extraction in oracle

我有程序将数据从远程数据库 SOAP_MONITORING table 提取到本地数据库 TEMP_SOAP_MONITORING table 中的每 5 min.The ID SOAP_MONITORING@FOINC_RETAIL table 是序列生成的。我将最大 ID 存储在捕获 table 中,并在接下来的 5 分钟提取中提取 ID 大于的数据,这样我将始终获得最新的 data.In Capturing table 我在 CAPTURED_DATE 列中执行提取时保存了日期时间戳。现在我唯一想做的是我还想在 LAST_CAPTURED_DATE 列中保存最后一次提取的日期时间,这样我就可以轻松地检查使用这两列进行数据提取所花费的时间。这是我的捕获 table:

ID  DB    DB_TABLE              LAST_TASK_ID        CAPTURED_DATE             LAST_CAPTURED_DATE   
181 FONIC TEMP_SOAP_MONITORING 201504211002095052   21.04.15 10:03:06         NULL

这是我的程序:

PROCEDURE "EXT_SOAP_MONITORING" AS 
LAST_SM_ID Number := 0;

BEGIN

--DELETE DATA FROM TEMP_SOAP_MONITORING
EXECUTE IMMEDIATE 'TRUNCATE TABLE TEMP_SOAP_MONITORING';

-- first retrieve the last id (of the newest record) which has been emportet at last extraction
SELECT LAST_TASK_ID INTO LAST_SM_ID FROM CAPTURING where DB_TABLE='TEMP_SOAP_MONITORING';

-- retrieve all new records from remote SOAP_MONITORING and insert it into TEMP_SOAP_MONITORING using MERGE statement
-- MERGE statement is able to retrieve CLOB fields:

merge into TEMP_SOAP_MONITORING TSM
using (
   select * from
(select ID,REQUEST_XML,RESPONSE_XML,WEB_SERVICE_NAME,WEB_METHOD_NAME,CREATE_DATE,ERROR_CODE,ERROR_MESSAGE from 
SOAP_MONITORING@FONIC_RETAIL WHERE WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' and ID > LAST_SM_ID order by ID desc) 
) data
ON (TSM.ID = data.ID)
when not matched then
insert(ID,REQUEST_XML,RESPONSE_XML,WEB_SERVICE_NAME,WEB_METHOD_NAME,CREATE_DATE,ERROR_CODE,ERROR_MESSAGE)
values(data.ID,data.REQUEST_XML,data.RESPONSE_XML,data.WEB_SERVICE_NAME,data.WEB_METHOD_NAME,data.CREATE_DATE,data.ERROR_CODE,data.ERROR_MESSAGE);



-- Set the newest extracted ID as LAST_TASK_ID for TEMP_SOAP_MONITORING in the Capturing table
update
CAPTURING set LAST_TASK_ID= 
CASE WHEN (SELECT MAX(ID) from TEMP_SOAP_MONITORING) IS NULL THEN LAST_TASK_ID + 1 ELSE (SELECT MAX(ID) from TEMP_SOAP_MONITORING) END,
CAPTURING_DATE = CURRENT_TIMESTAMP where DB_TABLE='TEMP_SOAP_MONITORING';

END EXT_SOAP_MONITORING1;

如果我对你的理解是正确的,你可以像对变量 LAST_SM_ID 那样做。 声明变量,在检索到 LAST_TASK_IDselect 中获取最后一个值,并在 update 语句中添加行。

PROCEDURE "EXT_SOAP_MONITORING" AS 
LAST_SM_ID Number := 0;
LAST_SM_DATE Timestamp;                         -- add this variable
BEGIN

...

-- store CAPTURED_DATE into variable LAST_SM_DATE
SELECT LAST_TASK_ID, CAPTURED_DATE INTO LAST_SM_ID, LAST_SM_DATE 
  FROM CAPTURING where DB_TABLE='TEMP_SOAP_MONITORING';

...

update CAPTURING set 
    LAST_TASK_ID= 
      CASE WHEN (SELECT MAX(ID) from TEMP_SOAP_MONITORING) IS NULL THEN LAST_TASK_ID + 1 
           ELSE (SELECT MAX(ID) from TEMP_SOAP_MONITORING) END,
    CAPTURING_DATE = CURRENT_TIMESTAMP,
    LAST_CAPTURED_DATE = LAST_SM_DATE          -- update here
  where DB_TABLE='TEMP_SOAP_MONITORING';

END EXT_SOAP_MONITORING1;