4
I get the following error :
[Err] ORA-06550: line 7, column 2:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
'Cause I’m using a create Quence inside my BEGIN with ; and / but it didn’t resolve the error.
DECLARE
v_idMax NUMBER(19,0);
BEGIN
select max(ID_PROD_PRECO_CUSTO_EMPRESA) INTO v_idMax from
ERP.EST_PROD_PRECO_CUSTO_EMPRESA;
linha 7 create sequence P_P_C_E_seq start with v_idMax increment by 1;
create or replace trigger PROD_INSERT
before insert on ERP.EST_PROD_PRECO_CUSTO_EMPRESA
for each row
begin
select P_P_C_E_seq.nextval into :new.ID_PROD_PRECO_CUSTO_EMPRESA from dual;
end;
FOR V_FUNC IN
(SELECT prod.ID_PRODUTO FROM ERP.EST_PRODUTO prod
INNER JOIN ERP.EST_PROD_PRECO_CUSTO_EMPRESA prodEmp on prodEmp.ID_PRODUTO = prod.ID_PRODUTO
GROUP BY prod.ID_PRODUTO
HAVING COUNT(prodEmp.ID_PRODUTO) !=83)
LOOP
FOR V_FUNC_EMPRESA IN (SELECT DISTINCT emp.ID_EMPRESA FROM ERP.CF_EMPRESA emp
WHERE EMP.ID_EMPRESA NOT IN (select emp.id_empresa FROM ERP.EST_PROD_PRECO_CUSTO_EMPRESA prd
INNER JOIN ERP.EST_PRODUTO prod ON prod.ID_PRODUTO = prd.ID_PRODUTO
INNER JOIN ERP.CF_EMPRESA emp ON prd.ID_EMPRESA = emp.ID_EMPRESA
WHERE prod.ID_PRODUTO = V_FUNC.ID_PRODUTO)
AND EMP.ID_EMPRESA != 110)
LOOP
INSERT
INTO EST_PROD_PRECO_CUSTO_EMPRESA
(
ID_PRODUTO,
ID_EMPRESA,
CUSTO_OPERACIONAL,
OUTRAS_DESPESAS,
PERCENTUAL_OUTRAS_DESPESAS,
PRECO_CUSTO,
PRECO_CUSTO_FINAL,
PRECO_CUSTO_MEDIO,
TOTAL_OUTROS_CUSTOS,
OUTROS_CUSTOS_ULTIMA_ENTRADA,
UUID,
DATA_ULTIMO_REAJUSTE
)
VALUES
(
V_FUNC.ID_PRODUTO,
V_FUNC_EMPRESA.ID_EMPRESA,
0,
0,
0,
0,
0,
0,
0,
0,
random_uuid(),
SYSDATE
)
;
END LOOP;
END LOOP;
drop sequence P_P_C_E_seq
drop trigger PROD_INSERT
END;
like I started studying PL/SQL yesterday so I’ll ask a few questions. I can do a Begin for each Run within the same DECLARE ? how:
begin
 Execute Immediate 'create sequence P_P_C_E_seq start' ...
end; / begin
 Execute Immediate 'create or replace trigger PROD_INSERT' ...
end;
– Guilherme Oliveira
only supplementing in the documentation says the following " Only Dynamic SQL can execute the following types of statements Within PL/SQL program Units: Data Definition language (DDL) statements such as CREATE, DROP, GRANT, and REVOKE Session control language (SCL) statements such as ALTER SESSION and SET ROLE " https://docs.oracle.com/cd/B19306_01/B14251_01/adfns_dynamic_sql.htm
– Daniel Gentil
@Guilhermeoliveira, answering the question you asked in the answer (it is better to delete it later or replicate it here in the comments). Unfortunately it is only possible to pass parameters in the immediate run to dml and plsql operations (Search for "using" if you want to know more about parameters in excute immediate). In your case you can use an replace in your script. Ex: EXECUTE IMMEDIATE replace('create Sequence P_p_e_seq start with :v_idMax increment by 1', ':v_idMax', to_char(v_idMax));
– Confundir
Guy I managed using || EX:
EXECUTE IMMEDIATE 'create sequence P_P_C_E_seq start with ' || (v_idMax + 1) ;
– Guilherme Oliveira