Alter table, add column / ORA-00984: column not allowed here PLSQL

梦想与她 提交于 2019-12-24 07:58:26

问题


The next statement SQL give me a "ORA-00984: column not allowed here":

ALTER TABLE USUVCB.TVCB_RUT_SII ADD (Fecha_Inicio VARCHAR2(10 BYTE) DEFAULT TO_CHAR(SYSDATE, "YYYY-MM-DD") NOT NULL);

It's into a PL-SQL, like this:

SET SERVEROUTPUT ON

DECLARE
Fecha VARCHAR2(8) := TO_CHAR (SYSDATE, 'YYYYMMDD');
Tabla VARCHAR2(28) := 'USER.TABLE_' || Fecha;
BEGIN
    SAVEPOINT START;
    BEGIN
        EXECUTE IMMEDIATE 'CREATE TABLE ' || Tabla || ' AS SELECT FIELD_1, FIELD_2, FIELD_3 FROM USER.TABLE';
    EXCEPTION
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Error creating the table');
    END;

    BEGIN
        EXECUTE IMMEDIATE 'ALTER TABLE USER.TABLE ADD (FIELD_4 VARCHAR2(10 BYTE) DEFAULT TO_CHAR (SYSDATE, "YYYY-MM-DD") NOT NULL)';
    EXCEPTION
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Error creating the field');
    END;

    BEGIN
        ...
    EXCEPTION
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('...');
    END;
EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Rollback');
    ROLLBACK TO START;
END;

I would like it to catch all exceptions ocurred into PL-SQL to could rollback at check-point START in case of any error.


回答1:


You need to use single quotes for the format mask:

ALTER TABLE USUVCB.TVCB_RUT_SII ADD (Fecha_Inicio VARCHAR2(10 BYTE) DEFAULT TO_CHAR(SYSDATE, 'YYYY-MM-DD') NOT NULL);

In an EXECUTE, this will be:

execute immediate 'ALTER TABLE USUVCB.TVCB_RUT_SII ADD (Fecha_Inicio VARCHAR2(10 BYTE) DEFAULT TO_CHAR(SYSDATE, ''YYYY-MM-DD'') NOT NULL)';

Notice that you are doing DDL queries, so you will not be able to rollback the modifications you made. A rollback only affects data, not the structure.

Besides, why do you store a date in a varchar column? it is a bad idea, it would be much better a date column




回答2:


Aleksej has a good solution. One often overlooked feature of Oracle is q quoting. By using this feature, you can use single quotes. Here is the same answer with q quoting:

EXECUTE immediate q'[ALTER TABLE USUVCB.TVCB_RUT_SII ADD (Fecha_Inicio VARCHAR2(10 BYTE) DEFAULT TO_CHAR(SYSDATE, 'YYYY-MM-DD') NOT NULL)]';



回答3:


I got my solution:

i am using

ALTER TABLE PG_PGS_MST_LABEL MODIFY (LANG_CODE DEFAULT EN );

but

my solution is that Lang_CODE column is varchar2 and i have to use single codes in EN i.e,

ALTER TABLE PG_PGS_MST_LABEL  MODIFY (LANG_CODE DEFAULT 'EN' );

thanks.



来源:https://stackoverflow.com/questions/39749577/alter-table-add-column-ora-00984-column-not-allowed-here-plsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!