运行整个SQL文件时出错。 (Oracle)的

时间:2013-04-24 00:25:08

标签: sql 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;

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

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

日志以“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;

2 个答案:

答案 0 :(得分:1)

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

  • 如果你进行那种操作,请确保选择语句 inside for循环实际上返回任何内容。
  • 如果你有外键,他们必须被禁用(从外键引用到禁用的主键导致错误)

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

答案 1 :(得分: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;
/