I want to read different sql statements from a CLOB field to HEX-code I then want to cast the HEX-code back within an a sql-script to varchar2 and to execute it. Generating HEX-code and casting works, but it does not execute. Can anyone please help me if and how Immediate Execute is possible?
Following example
- My sql statement I want in a CLOB field
drop table customers purge;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
- With a procedure I was given from a retired colleague I generated HEXCODE corretly using RAWTOHEX and UTL_RAW.CAST_TO_RAW. The HEXCODE is correct because the "decrypted" code using CAST_TO_VARCHAR2 looks correct. The following code shows the create statement from step 1:
SET LINESIZE 10000
SET serveroutput on size 300000 FORMAT WRAPPED
DECLARE
buffer clob;
BEGIN
buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('64726F70207461626C6520637573746F6D6572732070757267653B0A0A435245415445205441424C4520637573746F6D6572730A2820637573746F6D65725F6964206E756D62657228313029204E4F54204E554C4C2C0A2020637573746F6D65725F6E61');
buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('6D6520766172636861723228353029204E4F54204E554C4C2C0A202063697479207661726368617232283530292C0A2020434F4E53545241494E5420637573746F6D6572735F706B205052494D415259204B45592028637573746F6D65725F6964290A29');
buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('3B');
dbms_output.put_line(buffer);
END;
/
- What I am missing is that the code should not just be displayed, it should be excetuted so I got the messages Table dropped. and Table created. in this case.
My result:
SQL> SET LINESIZE 10000
SQL> SET serveroutput on size 300000 FORMAT WRAPPED
SQL>
SQL> DECLARE
2 buffer clob;
3
4 BEGIN
5
6 buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('64726F70207461626C6520637573746F6D6572732070757267653B0A0A435245415445205441424C4520637573746F6D6572730A2820637573746F6D65725F6964206E756D62657228313029204E4F54204E554C4C2C0A2020637573746F6D65725F6E61');
7 buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('6D6520766172636861723228353029204E4F54204E554C4C2C0A202063697479207661726368617232283530292C0A2020434F4E53545241494E5420637573746F6D6572735F706B205052494D415259204B45592028637573746F6D65725F6964290A29');
8 buffer := buffer||UTL_RAW.CAST_TO_VARCHAR2('3B');
9
10 dbms_output.put_line(buffer);
11
12 END;
13 /
drop table customers purge;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
PL/SQL procedure successfully completed.