Search notes:

Oracle: DBMS_SQL_TRANSLATOR

Functions and procedures

clear_sql_translation_error
create_profile Create an SQL translation profile (schema level) object
deregister_error_translation
deregister_sql_translation
drop_profile
enable_error_translation
enable_sql_translation
export_profile
import_profile
register_error_translation
register_sql_translation
set_dictionary_sql_id
set_error_translation_comment
set_sql_translation_comment
set_sql_translation_module
sql_hash Computes the hash value from the text of an SQL statement
sql_id Computes the sql id from the text of an SQL statement.
translate_error
translate_sql

Simple example

The specification of the PL/SQL package that translates SQL statements …
create or replace package renes_sql_translator as

   procedure translate_sql(
      sql_text             in   clob, 
      translated_text      out  clob
   );

   procedure translate_error(
      error_code           in   binary_integer,
      translated_code      out  binary_integer,
      translated_sqlstate  out  varchar2
   );

end renes_sql_translator;
/
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/spec.sql
… and its body:
create or replace package body renes_sql_translator as

   procedure translate_sql(
      sql_text             in   clob, 
      translated_text      out  clob
   )
   is begin
       translated_text := 'select * from (' || sql_text || ') where rownum < 2';
   end translate_sql;

 
   procedure translate_error(
      error_code           in   binary_integer,
      translated_code      out  binary_integer,
      translated_sqlstate  out  varchar2
   )
   is begin

      translated_code     := 42;
      translated_sqlstate :='what?';

   end translate_error;

end renes_sql_translator;
/
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/body.sql
Create a translation profile. Drop it, if it already exists
declare

   profile_name varchar2(32) := 'renes_sql_translation_profile';

begin

   begin
      dbms_sql_translator.drop_profile(profile_name);
   exception when others then
      if sqlcode = -24254 then null; end if;
   end;

   dbms_sql_translator.create_profile(profile_name);

   dbms_sql_translator.set_attribute(
      profile_name,
      dbms_sql_translator.attr_translator,
     'renes_sql_translator'
   );	

end;
/
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/profile.sql
Use the new profile in the current session:
alter session set sql_translation_profile = renes_sql_translation_profile;
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/set-profile.sql
Check the translation of an SQL statement.
declare
   sql_new clob;
begin
   dbms_sql_translator.translate_sql(
      'select * from dba_users',
       sql_new
   );

   dbms_output.put_line('Translated SQL is:');
   dbms_output.put_line('  ' || sql_new);
end;
/
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/get-translated.sql
Yes, this seems to be necessary in order to actually translate the text of an SQL statement. Setting event 10601 to level 32 specifies that the following SQL statement is one with foreign syntax (rather than native syntax). (TODO: This strange thing possibly goes away when setting attr_foreign_sql_syntax to false).
alter session set events = '10601 trace name context forever, level 32';
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/set-events.sql
Execute an SQL statement, let Oracle translate it and return the translated statement's result set:
select * from dba_objects;
Github repository oracle-patterns, path: /Installed/dbms/sql_translator/interface-package/select.sql

TODO

select * from dba_sql_translation_profiles;
select * from dba_objects where object_type = 'SQL TRANSLATION PROFILE';
alter session set sql_translation_profile = …;
alter session set events = '10601 trace name context forever, level 32';
ORA-01031: insufficient privileges
grant create sql translation profile to rene;

See also

The Oracle Object SQL Translation Profile
dbms_advanced_rewrite
Oracle DBMS PL/SQL packages

Index