Skip to content
Advertisement

multiple alter table in execute immediate

BEGIN
  execute immediate '
    BEGIN
        ALTER TABLE EMPRESA ADD CONSTRAINT pk_empresa_id PRIMARY KEY (EMPRESA_ID);

        ALTER TABLE CLIENTE ADD CONSTRAINT pk_cliente_id PRIMARY KEY (CLIENTE_ID);
    END';
END;

I want to do something like this in PL/SQL, but it throws me an error.

How should I do it?

Advertisement

Answer

Problem is that you can’t run DDL directly in PLSQL. You can run them in separate execute immediates:

BEGIN
    execute immediate 'ALTER TABLE EMPRESA ADD CONSTRAINT pk_empresa_id PRIMARY KEY (EMPRESA_ID)';
    execute immediate 'ALTER TABLE CLIENTE ADD CONSTRAINT pk_cliente_id PRIMARY KEY (CLIENTE_ID)';
END;
/
Advertisement