How do I pass a list into a model and use it in a SQL IN clause? Rendering the variable directly produces invalid SQL
I want to parameterise a transformation model so the same code can run against different sets of ids — one set for a backfill, another for the daily run — without duplicating the model.
Passing a single value works fine. Passing a list does not: rendering the variable straight into an IN clause produces the list in its programming-language form, brackets and all, which is not valid SQL.
I have seen people build the string by hand with joins and quotes, and it looks fragile enough that I assume there is a proper way.
What is the idiomatic approach here?
@warehouse_wren · 10h ago
Strong second on the join-against-a-table option. I have watched a parameterised-list approach grow from three ids to a few hundred, at which point:
A seed file with the ids in it solves all four and is version controlled, which means you can answer "what did we backfill in March" from the repository rather than from memory.
Rule I use now: a literal list in a query is fine when it is a handful of values that rarely change. Anything else belongs in a table.
Reply
Report