I am working on a legacy database which is Oracle 7 and I want to use the EXECUTE IMMEDIATE to execute the converted bytes to varchar but I found out the the said function was released on Oracle 10g.
Here is my example:
STORED PROCEDURE:
create or replace procedure QUERY_EXECUTOR
(
IN_QUERY_STATEMENT IN LONG RAW
)
AS
BEGIN
declare CONVERTED_QUERY varchar2(32767);
cursor c1 is select IN_QUERY_STATEMENT from dual;
BEGIN
for r2 in c1 loop
converted_query:=UTL_RAW.CAST_TO_VARCHAR2(substr(r2.IN_QUERY_STATEMENT,0));
dbms_output.put_line(converted_query);
end loop;
execute immediate converted_query; --THIS LINE
END;
END;
Look at execute immediate command. When I comment it out. The SP compiled successfully in Oracle 7.
But on Oracle 12g(which we are currently using), the sample code executes just fine.
EXEC QUERY_EXECUTOR('53454c45435420312046524f4d204455414c3b');
When converted is "SELECT 1 FROM DUAL;";
This conversion and execution is for us the best way to handle our current problem so answers will be very much appreciated.
Question
Is there a way or alternative solution if we cannot use the said command in Oracle 7?
EDIT:
Found this topic which is the other utility of executing literal string commands but it is only for DDL commands.
EDIT 2: SOLUTION
The answer by Sir @Wernfried Domscheit did the job for me. I combined the answer with my logic. Although it is not perfect and I think I need to read more of the documentation.
First, it seems that DBMS_SQL.VARCHAR2A
is not working in Oracle7.
Then I found this which having a different DBMS_SQL.PARSE
method signature. I used it because it says DBMS_SQL.V7
so it might be Oracle 7 but I really don't know. I need to read the documentations tho.
create or replace procedure QUERY_EXECUTOR
(
IN_QUERY_STATEMENT IN LONG RAW
)
AS
BEGIN
DECLARE
res INTEGER;
cur PLS_INTEGER := DBMS_SQL.OPEN_CURSOR;
CONVERTED_QUERY varchar2(32767);
cursor c1 is select IN_QUERY_STATEMENT from dual;
BEGIN
for r2 in c1 loop
converted_query:=UTL_RAW.CAST_TO_VARCHAR2(substr(r2.IN_QUERY_STATEMENT,0));
end loop;
DBMS_SQL.PARSE(cur, converted_query, DBMS_SQL.V7);
dbms_output.put_line(cur);
res := DBMS_SQL.EXECUTE(cur);
DBMS_SQL.CLOSE_CURSOR(cur);
END;
END;
PS:
I'm not a DBA so please if I'm doing something wrong on the solution part, can you give your opinion for much better solution? Very much appreciated the answer.