在sql server中的xquery的query()方法中出现问题 [英] Issue in query() method of xquery in sql server
问题描述
大家好,
当我试图解决算术表达式时,我在xquery的query()方法中遇到了问题。
问题是
当我使用这样的sql语句
DECLARE @x xml =' '
DECLARE @ x1 xml;
DECLARE @Result DECIMAL(38,8)
SET @ x1 = @ x.query ('1 * 12')
SET @ Result = CAST(@ x1.value('(/。)[1]','Float')AS DECIMAL(38,8) ))
选择@Result
然后它返回值12.00000000这是正确的
但当我打算使用任何变量代替这样的算术表达式
DECLARE @x xml =' '
DECLARE @ x1 xml;
DECLARE @Result DECIMAL(38,8)
DECLARE @r varchar(60)= '1 * 12'
SET @ x1 = @ x.query(@r)
SET @ Result = CAST(@ x1.value('( /。)[1]','Float')作为DECIMAL(38,8))
选择@Result
然后它给出错误
参数1 XML数据类型方法query必须是字符串文字。
i也试过
SET @ x1 = @ x.query(' sql:variable(@ r)')
但它只提供1 * 12而不是值。
请帮帮我。
Hi All,
I got a issue in query() method of xquery when i am trying to solve the arithmetic expression .
the problem is that
when i use the sql statments like this
DECLARE @x xml=''
DECLARE @x1 xml;
DECLARE @Result DECIMAL(38,8)
SET @x1= @x.query('1*12')
SET @Result= CAST(@x1.value('(/.)[1]', 'Float') AS DECIMAL(38,8))
Select @Result
then it returns the value 12.00000000 which is correct
but when i am going to use any variable in place of arithmetic expression like this
DECLARE @x xml=''
DECLARE @x1 xml;
DECLARE @Result DECIMAL(38,8)
DECLARE @r varchar(60)='1*12'
SET @x1= @x.query(@r)
SET @Result= CAST(@x1.value('(/.)[1]', 'Float') AS DECIMAL(38,8))
Select @Result
then it gives the error
The argument 1 of the XML data type method "query" must be a string literal.
i also tried
SET @x1= @x.query('sql:variable("@r")')
but it only gives only 1*12 not the value.
Please help me .
推荐答案
微软的帮助页明确指出(并且错误也告诉你)查询()无法获取它来自变量的参数!它必须有一个字符串文字...
http://msdn.microsoft .com / zh-CN / library / ms191474.aspx [ ^ ]
您可以尝试在此处创建动态查询...
Microsoft's help page clearly states (and the error also tells you) that query() can not get it's parameter from variable! It must have a string literal...
http://msdn.microsoft.com/en-us/library/ms191474.aspx[^]
You may try to create a dynamic query here...
这篇关于在sql server中的xquery的query()方法中出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!