Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2002

    Unanswered: Evaluating variable expressions using SQL


    Is there any way to substitute a value in an expression returned by the db? For example :

    I have an 'Expression' table of expressions like this:

    expressionid, expressionvalue
    1, '((x - 2) * 170.46) '
    2, '((360 * x) / 10)'
    3, '(x * 353)' and so on...

    I also have a 'SiteExpression' table with a siteid, expressionid mapping :

    siteid, expressionid
    1000, 2
    1001, 1
    1004, 3

    Now, I need to create a stored procedure.. that has the siteid as a parameter.. based on the siteid, I get the value 'x' from another table and I also get the expression id corresponding to the siteid.

    So I have 'x' and I have the expression, let's say '((360 * x) / 10)'

    Is there any function I can use to evaluate this expression? Or any other way? I cannot hard code the expression since the expression that I need to evaluate depends on the siteid passed in as a parameter.

    Thanks in advance!

  2. #2
    Join Date
    Jan 2007
    Jena, Germany
    You can take the expressions as string, replace "x" with your siteid (using REPLACE function, for example), then prepend the VALUES keyword and finally execute this statement as dynamic SQL. The VALUES returns a table with 1 row and 1 column, consisting of the expression result.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  3. #3
    Join Date
    Dec 2002
    thanks..will try it out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts