Thursday, February 26, 2015

XQuery syntax to evaluate math formula

I want to evaluate a math formula inside of a function, so I can't use dynamic SQL.

I found that the query on an XML-variable with a literal works well.




DECLARE @sParsedFormula varchar(200);
DECLARE @xFormula xml;
SET @xFormula = '';
SET @sParsedFormula = '1+2';
SELECT @xFormula.query('3+3') , @xFormula.query('sql:variable("@sParsedFormula")')

Output (1st value is correctly evaluated, 2nd not):

"6", "1+2"


I can't directly pass @sParsedFormula to the query otherwise I get "The argument 1 of the XML data type method "query" must be a string literal".

What is the correct XQuery-syntax to evaluate the SQL:variable?


Tx,

Patrick



No comments:

Post a Comment