title: Calculation based on custom formula, stored in a db table
body: <p>I have a table with custom functions (function name and list of parameters) and use it for calculation of a percentage of a cost for a product. I need this done is SQL (SQL Server 2008).</p>
  
  <p>This is the table to store custom functions </p>
  
  <pre><code>CREATE TABLE #tCustomFormula (customformula_id int, customfunction varchar(50), customparameters varchar(100))
  
  INSERT INTO #tCustomFormula(customformula_id, customfunction, customparameters)
  SELECT  1, 'xpCalcPercentage', 'customer_id,product_id,costperiod,productdate'
  -- xpCalcPercentage is custom formula to calculate cost of a product
  DROP TABLE #tCustomFormula
  </code></pre>
  
  <p>The calculation is done in stored procedure, where this table is queried with supplied customformula_id and list of parameters to stored function.</p>
  
  <p>For example:</p>
  
  <pre><code>exec spExecCustomFormula 
     1,   -- custom formula id from the table above
    '1,22,888,''2015-01-01''' -- parameters
  </code></pre>
  
  <p>OR, to match to a list of arguments in the table</p>
  
  <pre><code>exec spExecCustomFormula 
     1,   -- custom formula id from the table above
    'customer_id=1,product_id=22,costperiod=888,productdate=''2015-01-01'''  -- parameters
  </code></pre>
  
  <p>What would be the best way to define this stored procedure? Basically, I need to match list of parameters to stored procedure to the customparameters field from table #tCustomFormula, construct the executeSQL string and using dynamic SQL, execute it.</p>
  
  <p>Any help greatly appreciated.</p>
  
  <p>Thanks,
  Gena</p>
  
  <p>=========================================================================</p>