How to evaluate expression in select statement in Postgres

后端 未结 2 1289
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 03:21

Postgres 9.1+ database contains customers and product. In customers table, customer price is described as sql expression in priceexpression column for every customer.

H

2条回答
  •  灰色年华
    2021-01-26 03:44

    You can't do this with plain SQL in PostgreSQL.

    You'll need to use dynamic SQL with PL/PgSQL's EXECUTE statement. See PL/PgSQL and the many examples here on Stack Overflow.

    Loop over the result set and EXECUTE 'SELECT '||the_expression INTO resultvar; for each row.

    Be aware that this is a massive security hole if anyone who isn't truested to run raw SQL can possibly modify the price column. You should really not do this.

提交回复
热议问题