2 ms·
> Further: SQL is not composable. If I have a fragment of SQL in my hand, there is no reasonable way to insert that into a larger expression. This is really an
by lmz 16y ago
> Further: SQL is not composable. If I have a fragment of SQL in my hand, there is no reasonable way to insert that into a larger expression.
This is really annoying. If I have a stored proc for getting data w/ 3 optional parameters for filtering it, I have to either use dynamic SQL (building the query by string concatenation), or have 8 different versions of the query for each combination of filter present/not present. The dynamic SQL part might not even work with bind parameters, because EXECUTE IMMEDIATE / sp_executesql cannot take a dynamic number of bind parameters (0 to 3 bind parameters, depending on the filter).
I have to wonder why none of the database vendors ever created a syntax-tree API for SQL queries. They have the parser right there, so why can't they do something like
q := PARSE(SELECT x, y, z FROM sometable);
q.ADD_CONDITION(PARSE(sometable.value = some_dynamic_value));
EXECUTE q;