Oracle regularly clears the table data half a year ago (the table structure is the same, one GPS data table per day)

Ideas:

1. Query the table half a year ago
            select table_name from user_tables where table_name in (
            select TABLE_NAME from (
            select u.TABLE_NAME from user_tables u,
            (select add_months(sysdate,-6) half_year from dual) a
            where u.LAST_ANALYZED<=half_year
            order by u.LAST_ANALYZED)) and table_name like '%筛选表名%';
2. Create a stored procedure
create or replace procedure delete_gps
AS
res varchar2(30);--The variable used to process the result
sql_text varchar2(100);--store dynamic sql
-- declare a cursor
Cursor tableNameCursor is
--Query the GPS data sheet six months ago
select table_name from user_tables where table_name in (
select TABLE_NAME from (
select u.TABLE_NAME from user_tables u,
(select add_months(sysdate,-6) half_year from dual) a
where u.LAST_ANALYZED<=half_year
order by u.LAST_ANALYZED)) and table_name like '%GPS_V1%';
begin
LOOP
   -- open cursor
  IF NOT tableNameCursor%ISOPEN  THEN
     OPEN tableNameCursor;
  END IF;
  FETCH tableNameCursor INTO  res;
 --Condition to exit the loop
  EXIT WHEN tableNameCursor%NOTFOUND OR tableNameCursor%NOTFOUND IS NULL;
  sql_text := 'truncate TABLE '|| res ; -- splicing dynamic sql
  execute immediate (sql_text); -- execute dynamic sql
END LOOP;
end;

Test execution stored procedure
call stored procedure name
3. Create job timing execution

(1), fill in the stored procedure to be executed in the what value, multiple can be separated

  (2), fill in the execution time strategy in the interval; (see the following remarks for the specific writing method)

Note: Job timing execution and time interval description

Interval/ interval refers to the time interval from the end of the previous execution to the start of the next execution. When the interval is set to null, the job will be deleted from the queue after the execution ends. If we need the job to be executed periodically, use 'sysdate+m'. (1). Execute Interval => TRUNC(sysdate,'mi') + 1/ (24*60)
every minute

Execute every hour

Interval => TRUNC(sysdate,'hh') + 1/ (24)

(2). Regular execution every day. For example: execute Interval => TRUNC(sysdate+ 1) +1/ (24) (3)
at 1 am every day . Weekly timed execution for example: Interval => TRUNC(next_day(sysdate,'Monday'))+1/24 (4). Monthly timed execution for example: Interval is executed at 1:00 am on the 1st of each month =>TRUNC(LAST_DAY(SYSDATE))+1+1/24 (5). Regular execution every quarter. For example, Interval => TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 1/24 (6). Regular execution every six months. For example: every July 1st and January 1st at 1:00 AM Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+1/24 (7) . Regular execution every year For example: Interval =>ADD_MONTHS(trunc(sysdate,'yyyy'),12)+1/24 every year at 1:00 am on January 1st






















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324641717&siteId=291194637