database cells: can I inject references to other cells?

If I am running a query within a database cell like SELECT * FROM table_name , is it still possible to pass in a value from another cell? I would like to pass in table_name as a variable , such as with viewof variable = Inputs.select(array_with_table_names) . I don’t see how to ‘escape’ into JavaScript in a database cell using patterns like SELECT * FROM ${variable}

You can reference a dynamic value, but in most database client implementations (including PostgreSQL), you can’t reference a dynamic table or column or syntax.

So, you can say

SELECT * FROM users WHERE name = ${x};

but you can’t say

SELECT * FROM ${x} WHERE name = 'foo';

and you also can’t say something like

SELECT ${x} FROM users WHERE name = 'foo';

If you want to do those things, you could construct the SQL query dynamically, but you can’t do that using an Observable SQL cell (or the db.sql tagged template literal). You’d have to construct the SQL yourself, and you’d be responsible for any escaping.

2 Likes

Thank you!

but you can’t do that using an Observable SQL cell (or the db.sql tagged template literal) [emphasis added]

… just a quick note: it seems you can do this? :slight_smile:

i.e.

db_attribute_data = db.query(`SELECT * FROM ${db_explore}`)

totally works!

… (though after posting I realize I am likely mistaken, as I am using a JavaScript cell rather than the db.sql tagged template literal)