Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a dot separated table as a parameter into the sql query

For purposes of this question, let's say there is a table schema foo.bar.baz

And we have created a cursor object using following boilerplate

import snowflake.connector

ctx = snowflake.connector.connect(...)
cur = ctx.cursor()

With that cursor object, we can put the whole dot deliminated schema into a query like so:

cur.execute('''
select * from foo.bar.baz
'''
)

and have no issues, but we wouldn't be able to do:

cur.execute('''
select * from %(tbl)s
''', {'tbl': 'foo.bar.baz'} 
)

Doing that throws this type of error: ProgrammingError: 001011 (42601): SQL compilation error: invalid URL prefix found in: foo.bar.baz

I'm guessing this is because the dots are sql identifiers and not strings, but I don't see any workaround in the snowflake documentation. Does anyone know how this could be done without having to change the connection object.

like image 749
Maksim Avatar asked Nov 25 '25 07:11

Maksim


1 Answers

Using TABLE:

In a FROM clause, the syntax TABLE( { string_literal | session_variable | bind_variable } ) can be used

select * from TABLE(%(tbl)s)
like image 75
Lukasz Szozda Avatar answered Nov 26 '25 23:11

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!