0

我无法完整地运行我的 SQL 脚本,即使我单独选择它们很容易运行的语句。

我相信问题出在我添加的块上。

其中之一是:

begin
for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'
and status = 'ENABLED') LOOP
execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
end loop;
end;

同样,我可以毫无故障地单独运行每个段,甚至整个文件也可以减去这些代码块。但是当我添加这些代码块时,它不会运行。

编辑:对不起,这里有错误和更好的解释。

日志以“old:begin”开始,然后记录整个脚本而不是运行它。错误本身是:

Error report:
ORA-06550: line 11, column 1:
PLS-00103: Encountered the symbol "TRUNCATE" 
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

这是没有 INSERT 语句的代码,因为我知道它们可以正常工作。

/*
 * NOTE: For some reason I have yet to figure out, I can not run the Disable Foreign Keys or
 *       Enable Foreign Keys blocks with the entiriry of the script. So I have been having to
 *       disable them, run the truncates, and then enable them. Then, and only then, can I
 *       run the rest of the INSERT statements.
 */

/*
 * My New Cat Project
 */

/*
 * DISABLE FOREIGN KEYS
 *
 * CODE USED FROM: 
 * http://blog.yourdotnetsolution.com/2008/05/how-to-disable-or-enable-all-foreign.html
 */
begin
for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'
and status = 'ENABLED') LOOP
execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
end loop;
end;

/*
 * TRUNCATE TABLES AND DROP SEQUENCES
 */
TRUNCATE TABLE CARE; 
TRUNCATE TABLE FAVORITES;
TRUNCATE TABLE MEDICINE_PRESCRIBED;
TRUNCATE TABLE MEDICATION;
TRUNCATE TABLE MEDICAL_CONDITIONS;
TRUNCATE TABLE MEDICAL_INFO;
TRUNCATE TABLE SURGERY;
TRUNCATE TABLE TREATMENT;
TRUNCATE TABLE VET_VISIT;
TRUNCATE TABLE VET;
TRUNCATE TABLE CAT_RECEIVED;
TRUNCATE TABLE RELINQUISHER;
TRUNCATE TABLE CAT_FOUND;
TRUNCATE TABLE ADOPTION;
TRUNCATE TABLE ADOPTER;
TRUNCATE TABLE EVENT;
TRUNCATE TABLE DONATIONS;
TRUNCATE TABLE DONOR;
TRUNCATE TABLE MILEAGE;
TRUNCATE TABLE FCP_EXPENSE;
TRUNCATE TABLE EXPENSE_TYPE;
TRUNCATE TABLE FCP_INITIALIZATION;
TRUNCATE TABLE FOSTER_CARE_PROVIDER;
TRUNCATE TABLE ORGANIZATION;
TRUNCATE TABLE CAT_TEMPERAMENTS;
TRUNCATE TABLE CAT;

DROP SEQUENCE CAT_SEQ;
DROP SEQUENCE CAT_TEMPERAMENTS_SEQ;
DROP SEQUENCE ORGANIZATION_SEQ;
DROP SEQUENCE FOSTER_CARE_PROVIDER_SEQ;
DROP SEQUENCE FCP_INITIALIZATION_SEQ;
DROP SEQUENCE EXPENSE_TYPE_SEQ;
DROP SEQUENCE FCP_EXPENSE_SEQ;
DROP SEQUENCE MILEAGE_SEQ;
DROP SEQUENCE DONOR_SEQ;
DROP SEQUENCE DONATIONS_SEQ;
DROP SEQUENCE EVENT_SEQ;
DROP SEQUENCE ADOPTER_SEQ;
DROP SEQUENCE ADOPTION_SEQ;
DROP SEQUENCE CAT_FOUND_SEQ;
DROP SEQUENCE RELINQUISHER_SEQ;
DROP SEQUENCE CAT_RECEIVED_SEQ;
DROP SEQUENCE VET_SEQ;
DROP SEQUENCE VET_VISIT_SEQ;
DROP SEQUENCE TREATMENT_SEQ;
DROP SEQUENCE SURGERY_SEQ;
DROP SEQUENCE MEDICAL_INFO_SEQ;
DROP SEQUENCE MEDICAL_CONDITIONS_SEQ;
DROP SEQUENCE MEDICATION_SEQ;
DROP SEQUENCE MEDICINE_PRESCRIBED_SEQ;
DROP SEQUENCE FAVORITES_SEQ;
DROP SEQUENCE CARE_SEQ;

/*
 * ENABLE FOREIGN KEYS
 *
 * CODE USED FROM: 
 * http://blog.yourdotnetsolution.com/2008/05/how-to-disable-or-enable-all-foreign.html
 */
begin
for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'
and status = 'DISABLED') LOOP
execute immediate 'alter table '||i.table_name||' enable constraint '||i.constraint_name||'';
end loop;
end; 

/*
 * CREATE SEQUENCES
 */

/*
 * CAT
 */
CREATE SEQUENCE CAT_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * CAT TEMPERAMENTS
 */
CREATE SEQUENCE CAT_TEMPERAMENTS_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;


/*
 * ORGANIZATION
 */
CREATE SEQUENCE ORGANIZATION_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;


/*
 * FOSTER CARE PROVIDER
 */
CREATE SEQUENCE FOSTER_CARE_PROVIDER_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * FCP INITIALIZATION
 */
CREATE SEQUENCE FCP_INITIALIZATION_SEQ
    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;


/*
 * EXPENSE TYPES
 */
CREATE SEQUENCE EXPENSE_TYPE_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * FOSTER CARE PROVIDER: EXPENSES
 */
CREATE SEQUENCE FCP_EXPENSE_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * MILEAGE
 */
CREATE SEQUENCE MILEAGE_SEQ

    MINVALUE            1
    INCREMENT BY        1
    CACHE               20;

/*
 * DONORS
 */
CREATE SEQUENCE DONOR_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * DONATIONS
 */
CREATE SEQUENCE DONATIONS_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * EVENTS
 */
CREATE SEQUENCE EVENT_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * ADOPTER
 */
CREATE SEQUENCE ADOPTER_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * ADOPTION
 */
CREATE SEQUENCE ADOPTION_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * CATS FOUND
 */
CREATE SEQUENCE CAT_FOUND_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * RELINQUISHERS
 */
CREATE SEQUENCE RELINQUISHER_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * CATS RECEIVED
 */
CREATE SEQUENCE CAT_RECEIVED_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * VET
 */
CREATE SEQUENCE VET_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * VET VISITS
 */
CREATE SEQUENCE VET_VISIT_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * TREATMENTS
 */
CREATE SEQUENCE TREATMENT_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * SURGERY
 */
CREATE SEQUENCE SURGERY_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * MEDICAL INFO
 */
CREATE SEQUENCE MEDICAL_INFO_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * MEDICAL CONDITIONS
 */
CREATE SEQUENCE MEDICAL_CONDITIONS_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;


/*
 * MEDICATION
 */
CREATE SEQUENCE MEDICATION_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * MEDICINE PRESCIBED
 */
CREATE SEQUENCE MEDICINE_PRESCRIBED_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;


/*
 * FAVORITES
 */
CREATE SEQUENCE FAVORITES_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;

/*
 * CARE
 */
CREATE SEQUENCE CARE_SEQ

    MINVALUE        1
    INCREMENT BY    1
    CACHE           20;
4

2 回答 2

1

由于没有关于从您的块返回的错误代码的信息 - 没有详细的答案,只有一般建议。

  • 如果您进行这种操作,请确保 for 循环中的 select 语句实际上返回任何内容。
  • 如果您有外键,则必须禁用它们(从外键引用到禁用的主键会导致错误)

立即执行也必须包含有效的 DDL。

于 2013-04-24T06:32:49.930 回答
0

/从 Oracle 中的 SQL 文件运行时,函数需要结束。例如:

begin
for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'
and status = 'ENABLED') LOOP
execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
end loop;
end;
/
于 2013-04-24T20:42:13.200 回答