1

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

How to create price list from this data ? I tried code below but got error since eval() is undefined.

create table customer
( id int primary key,
  priceexpression text );
insert into customer values (1, 'price*0.95'),(2,'cost+12.0' );

create table product
( id char(20) primary key,
   price numeric(12,4),
   cost numeric(12,4) );
insert into product values ('PRODUCT1', 120, 80),('PRODUCT2', 310.5, 290);


select
  customer.id as customer,
  product.id as product,
  eval(priceexpression) as price
 from customer,product

This is ASP.NET MVC4 application.

2 Answers 2

1

you can write an SQL function that does this for you and use e.g. the ones supplied with postgres-utils:

select 
  c.name as cust_name,
  p.name as prod_name,
  p.cost as prod_cost,

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

from product p,  customer c

But of course it may be slow, insecure, you could use materialized views to cache it more easily, etc. - see docu there.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. Great. I marked it as answer and upvoted.
I tried but got error. I posted it in stackoverflow.com/questions/36939625/…
I updated my answer for your problem (I did not actually try it out myself with your example!) ... you just have to add all your parameters and tables necessary for your specific "dyna-sql" (product and :cid). better use :cost instead of {cost} in your data since it is more convenient, understandable and does not conflict with array syntax.
ahh ... can't edit last comment anymore :-/ ... must be :pid of course (instead :cid)
You can delete comment and enter it again as new. Columns are added to customer and product tables at runtime and expressions are also created at runtime. How to pass all columns automatically to eval, without using hard-coded parameters? plpgsql has row type, maybe this can used
|
1

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.

2 Comments

Based on accepted answer it may be doable. I posted realted question in stackoverflow.com/questions/36939625/…
Um, yes. You're using PL/PgSQ with EXECUTE. As I advised above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.