I'm trying to extract all the table names in a query using sqlparse but I'm having trouble with subqueries or insert statements wrapped in parenthesis.
I have the following query,
import sqlparse
sql = """
create test.test_table as (
select 1
from fake.table
);
"""
When I look for the token that included the statement in the parenthesis using
y = sqlparse.parse(sql)
for i in y[0].tokens:
if isinstance(i, Identifier):
print(i)
print(i.get_real_name())
I get the following results,
test.test_table as (
select 1
from fake.table
)
test_table
The results come back as just one identifier token. When I attempt to get the table name from inside the parenthesis all the comes back is test.test_table. What I'm ultimately trying to do is extract both table names test.test_table and fake.table
Does anyone have any insight on how I can go about doing this?