Use variables for FROM clause

I would like to use variable for FROM clause. I tried this:

select * 
from public."{{table_name}}"
limit 10

but it does not work. Also I created a view consisting of table names so I can easily create a filter for the table names but it does not work either.

How to accomplish this?

AFAIK, this is not possible.

You can read about SQL parameters here.

Depends upon your database. You may be able to create a function that accepts a table name, then returns the rows. You'd then need to do something like

select * from myfunction(tablename)

All the accessible table names will need to be UNION compatible (same column names and data types).
Would be better to redefine the problem though.

1 Like