How to fix postgres-utils eval() error: missing FROM-clause entry for table “foo”?

半城伤御伤魂 提交于 2019-12-06 16:40:06
Andreas Dietrich

Serg is basically right. Your dyna-SQL is executed "on its own" so it needs to be a valid SQL statement (having "to know" all involved tables). I updated my answer in the referred thread to reflect this.

But to properly cite it in here your example should be something like (actually you should use the 2nd eval( sql text, keys text[], vals text[] ) variant!):

eval(  
  'select '||c.price_expression||' from product where id=:pid',
  '{"{cost}",:pid}',  
  array[ p.cost, p.id ]  
)      as cust_cost

This should be more straight forward, robust and modular than Sergs suggestions.

Just add the table name somwhere. May be

insert into customer values (1, 'product.price*0.95 FROM product'),(2,'cost+12.0 FROM product' );

or may be

 execute 'select ' ||   sql || ' FROM product' into  as_txt ;

at your choice.

Hope this priceexpression is not exposed to users but only to restricted number of admins, because it's dangerous sql injection security hole.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!