Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Christian.

Asked: April 16, 2026 - 7:45 am UTC

Last updated: April 21, 2026 - 1:24 pm UTC

Version: 19.19

Viewed 1000+ times

You Asked

Hi,

I have a database with thousands of tables containing the same kind of information.

I need to write a program to aggregate these informations and thought about using a sqlmacro.

-- This is a very simplified concept
create or replace
    function get_val (p_table_name varchar2)
           return varchar2 SQL_Macro 
    is
      return 'select col1,col2 from p_table_name'; 
    end;
/

select col1, col2
  from get_val(t.table_name)
     , table_list t;  --Table_list contains the list of the table to take



And it always tells that the table doesn't exist.

The documentation talks about DBMS_TF.TABLE_T, which works if you pass the table as the parameter (and not the table's name).

How can I do that? Do I have to write a function returning the rows from the table?

Thank you

and Chris said...

You need to pass tables to macros using the dbms_tf.table_t parameter. When calling the macro, you pass the table name itself, not a string containing the table name.

For example, this fails because the table_name parameter is a string:

create or replace function mcro ( table_name varchar2 ) 
  return clob sql_macro
as
begin 
  return 'select count(*) from table_name';
end;
/

select * from mcro ( 'employees' );
ORA-00942: table or view "CHRIS"."TABLE_NAME" does not exist


Instead of replacing the table_name with the parameter valud, the query searches for a table called "TABLE_NAME" (as the error shows).

Here's the revised version which enables you to pass the table:

create or replace function mcro ( table_name dbms_tf.table_t ) 
  return clob sql_macro
as
begin 
  return 'select count(*) from table_name';
end;
/

select * from mcro ( employees );


So you can't use rows in another table to pass the table names to the macro.

If you want to do this, you could either rewrite the function to use dynamic SQL

Or generate a script of the macro calls with a query like:

select  
 ' select * from mcro ( "' || table_name || '" ); '
from user_tables;

Rating

  (2 ratings)

Comments

That's bad...

Christian, April 16, 2026 - 10:45 am UTC

Thank you for the unfortunate confirmation.

As I see a lot of professional software use thousands of parameter or configuration tables, it would be useful.

So, I will have to write a function for that.

Thank again for you help. I look at asktom for the last 20 years with interest.


Connor McDonald
April 20, 2026 - 2:47 am UTC

There's some enhancements to macros coming in upcoming releases, but I don't think we'll ever head down a "pass a table as a string" path, because that has SQL injection risks all over it

Bind variables and SQL Macros

Rajeshwaran Jeyabal, April 21, 2026 - 10:51 am UTC

Team - Let's say i have a below Macro function in the database

How can this be called from Application or PL/SQL code using bind variables for input parameters ?

create or replace function foo(
 p_tab in dbms_tf.table_t
 , p_col in dbms_tf.columns_t )
return varchar2
sql_macro
as
begin 
 ............
 ............
 ............
end;
/ 

Chris Saxon
April 21, 2026 - 1:24 pm UTC

You can't - table and column names have to be identifiers. For columns, this is in a list inside the COLUMNS pseudo operator

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here