Hi I’m using python 3 and snowflake. Example:
JavaScript
x
8
1
MY_SCHEMA = 'TEST'
2
MY_TABLE = Empty
3
4
sql = "SELECT SCHEMA, TABLE FROM TABLE_LOG WHERE SCHEMA = %s AND TABLE = %s"
5
6
tuple1 = (MY_SCHEMA, MY_TABLE)
7
cur.execute(sql,tuples1)
8
I tried with this:
JavaScript
1
2
1
WHERE SCHEMA = %s OR (SCHEMA IS NULL AND %s IS NULL) also WHERE SCHEMA = IIF(%S IS NULL, SCHEMA, %s)
2
I’m getting TypeError: not enough arguments for format string. I would like to run this query for all schemas if I dont have defined MY_SCHEMA variable and also for all tables if MY_TABLE variable is null or empty. I would be grateful for any advice.
Advertisement
Answer
I found a solution this issue:
JavaScript
1
6
1
MY_SCHEMA = 'TEST'
2
MY_TABLE = None
3
4
cur.execute("SELECT SCHEMA,TABLE FROM TABLE_LOG WHERE SCHEMA = COALESCE(%(schema_variable)s,SCHEMA) AND TABLE = COALESCE(%(table_variable)s,TABLE )",{"schema_variable": MY_SCHEMA, "table_variable": MY_TABLE})
5
6