ORACLE 10g:从远程数据库重置序列

时间:2017-05-18 07:48:59

标签: sql oracle plsql oracle10g

我有需要复制的远程数据库,供团队使用。我创建了一个数据库链接和一个同义词,可以从远程访问all_sequences表。

create synonym rprod_all_sequences for all_sequences@rprod_production;

现在我创建了一个sql文件,该文件将获取所有用户序列并将其更改为以正确的数字开头。

set serveroutput on
/******************************************************************************/
/* RESET ALL SEQUENCE NUMBERS                                                 */
/******************************************************************************/

declare 
cursor c_cursor is 
  select sequence_owner||'.'||sequence_name AS name, TO_CHAR(last_number + 1) AS last_number
    from rprod_all_sequences
     where sequence_owner not in ('WMSYS','XDB','SYS','SYSTEM','SYSMAN','MDSYS','LBACSYS','EXFSYS','DMSYS','DBSNMP','CTXSYS');

begin
 for r_record in c_cursor loop
    execute immediate 'alter sequence ' || r_record.name || ' ' ||' INCREMENT BY '|| r_record.last_number ||';';
  end loop;

end;
/
exit;

但我总是收到这个错误:

sqlplus@RPROD> @reset_seq.sql
declare
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at line 7

我不确定为什么它会抱怨脚本,因为我单独尝试它并且它正在工作。对不起,这可能是一个菜鸟问题。关于为什么的任何想法?

感谢。

1 个答案:

答案 0 :(得分:1)

您在执行立即声明的末尾有一个额外的;。试试这个:

 DECLARE
   CURSOR c_cursor
   IS
      SELECT sequence_owner || '.' || sequence_name AS name,
             TO_CHAR (last_number + 1) AS last_number
        FROM rprod_all_sequences
       WHERE sequence_owner NOT IN ('WMSYS',
                                    'XDB',
                                    'SYS',
                                    'SYSTEM',
                                    'SYSMAN',
                                    'MDSYS',
                                    'LBACSYS',
                                    'EXFSYS',
                                    'DMSYS',
                                    'DBSNMP',
                                    'CTXSYS');
BEGIN
   FOR r_record IN c_cursor
   LOOP
      EXECUTE IMMEDIATE
            'alter sequence '
         || r_record.name
         || ' '
         || ' INCREMENT BY '
         || r_record.last_number
          ;
   END LOOP;
END;
/