We have a use-case where we need to pivot the result set of a query to columns for insert statement. For that, we are using crosstab which takes text sql as a parameter.
so the query might conceptually look like this:
insert into table(col1, col2, ...)
select col1, col2, ...
from crosstab($$
select ....
where something = {something}
$$)
...
in postgresql client, it all works perfectly fine.
Now, when we are trying to implement this in code with Anorm, it would look as follows:
val sql = $"""
|insert into table(col1, col2, ...)
|select col1, col2, ...
|from crosstab($$$$
| select ....
| where something = {something}
|$$$$)
|...
"""
SQL(sql).on("something" -> param).execute()
This should work, but it does not. The reason is that in this particular case, parameters defined in the internal SQL string is not getting replaced with the actual value.
Of course, it is possible to build the SQL string by hand using string interpolation. But, I would prefer to use the tools that built for it.
What is there a way to make the parameters replacements work in this case?