PLSQL循环通过100行一次删除

时间:2017-10-31 02:00:37

标签: plsql

我尝试在下面的代码中获取1000行并循环删除每行100行。

DECLARE 
COUNTER INTEGER :=0;
v_delstr varchar2(2000);
v_countstr varchar2(2000);
SchemaName Varchar2(40):='Staging';
TableName Varchar2(100):='History';
 BEGIN
LOOP
COUNTER := COUNTER + 1;
DBMS_OUTPUT.PUT_LINE('Counter: '); 
v_delstr:= 'Delete FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 1000 And startdate>=sysdate-731 and  startdate<=sysdate-730';

 execute immediate v_delstr;
 dbms_output.put_line(v_delstr);

v_countstr:= 'BEGIN 
                DECLARE CANT INTEGER; 
                      SELECT COUNT(*) INTO CANT FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 1000 And startdate>=sysdate-731 and startdate<=sysdate-730;
                      EXIT WHEN CANT <= 100;  
                      COMMIT;  
              END;';
execute immediate v_countstr;
dbms_output.put_line(v_countstr);
  --dbms_output.put_line(v_delstr);
  --dbms_output.put_line(v_countstr);
END LOOP;
END;
DECLARE 
COUNTER INTEGER :=0;
v_delstr varchar2(2000);
v_countstr varchar2(2000);
SchemaName Varchar2(40):='Staging';
TableName Varchar2(100):='History';
 BEGIN
LOOP
COUNTER := COUNTER + 1;
DBMS_OUTPUT.PUT_LINE('Counter: '); 
v_delstr:= 'Delete FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 10 And startdate>=sysdate-731 and  startdate<=sysdate-730';

 execute immediate v_delstr;
 dbms_output.put_line(v_delstr);

v_countstr:= 'BEGIN 
                DECLARE CANT INTEGER; 
                      SELECT COUNT(*) INTO CANT FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 10 And startdate>=sysdate-731 and startdate<=sysdate-730;
                      EXIT WHEN CANT <= 1;  
                      COMMIT;  
              END;';
execute immediate v_countstr;
dbms_output.put_line(v_countstr);
  --dbms_output.put_line(v_delstr);
  --dbms_output.put_line(v_countstr);
END LOOP;
END;

收到错误

Error report -
ORA-06550: line 3, column 23:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

   begin function pragma procedure subtype type <an identifier>
   <a double-quoted delimited-identifier> current cursor delete
   exists prior
The symbol "begin" was substituted for "SELECT" to continue.
ORA-06550: line 6, column 18:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   ( begin case declare end exception exit for goto if loop mod
   null pragma raise return select update while with
   <an identifier> <a double-quoted delimited-identifier>
   <a bind variable> << continue close current delete fetch lock
   insert open rollback savepoint set sql execute commit forall
   merge pipe purge
ORA-06512: at line 22
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

1 个答案:

答案 0 :(得分:0)

我不确定你对代码的目的是什么,但试试这个。它最有可能工作。

    DECLARE
      COUNTER    INTEGER :=0;
      v_delstr   VARCHAR2(2000);
      v_countstr VARCHAR2(2000);
      SchemaName VARCHAR2(40) :='Staging';
      TableName  VARCHAR2(100):='History';
    BEGIN
      FOR COUNTER IN 0..9
      LOOP
        COUNTER := COUNTER + 1;
        DBMS_OUTPUT.PUT_LINE('Counter: ');
        v_delstr:= 'Delete FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 1000 And startdate>=sysdate-731 and  startdate<=sysdate-730';
        EXECUTE immediate v_delstr;
        dbms_output.put_line(v_delstr);
        v_countstr:= 'DECLARE                 
    CANT INTEGER;                  
    BEGIN                       
    SELECT COUNT(*) INTO CANT FROM ' ||SchemaName ||'.'||TableName|| ' where  rownum <= 1000 And startdate>=sysdate-731 and startdate<=sysdate-730;                      
    EXIT WHEN CANT <= 100;                        
    COMMIT;                
    END;';
        EXECUTE immediate v_countstr;
        dbms_output.put_line(v_countstr);
        --dbms_output.put_line(v_delstr);
        --dbms_output.put_line(v_countstr);
      END LOOP;
    END;