问题描述:

I have a form where people can type in a start and end date, as well as a column name prefix.

In the backend, I want to do something along the lines of

SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS ({{prefix}} + '_startDate')

Is this possible? Basically, I want to dynamically create the name of the new column. The table is immediately returned to the user, so I don't want to mutate the underlying table itself. Thanks!

网友答案:

You can execute dynamic query that you have prepared by using EXECUTE keyword, otherwise it is not possible to have dynamic structure of SQL.

Since you are preparing your SQL outside database, you can use something like:

SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS {{prefix}}_startDate

Assuming that {{prefix}} is replaced with some string by your template before it is sent to database.

相关阅读:
Top