简体   繁体   English

传递给SQL Server XML数据类型方法的XQuery的类型是什么?

[英]What is the type of XQuery passed to SQL Server XML Data Type methods?

According to BOL on value() Method (xml Data Type) , 根据BOL的value()方法(xml数据类型)

value() method takes two arguments value()方法有两个参数

  • XQuery XQuery
  • SQLType SQL类型

Do I need to pass varchar or nvarchar to value() ? 我需要将varcharnvarchar传递给value()吗? How can I find out what kind of type XQuery or SQLType expects? 如何找出XQuery或SQLType期望的类型?

End Goal : To create utility UDF/sprocs that uses XML Data Type methods . 最终目标 :创建使用XML数据类型方法的实用工具UDF / sprocs。

It works with both: 可以同时使用:

WITH    q AS
        (
        SELECT  CAST('<root><node>1</node></root>' AS XML) AS doc
        )
SELECT  doc.value('(/root/node)[1]', 'INT'),
        doc.value(N'(/root/node)[1]', 'INT')
FROM    q

Note that the XQuery is compiled during the parsing stage, ie you can supply only the string literal as first argument (not a column, expression or a variable). 请注意, XQuery是在解析阶段编译的,即,您只能提供字符串文字作为第一个参数(而不是列,表达式或变量)。

This string literal has no "type", since type assumes a set of possible values that are known only at runtime, and XML functions accept only literal XQuery expressions which should be known at compile time. 此字符串文字没有“ type”,因为type假定了一组可能在运行时才知道的可能值,并且XML函数仅接受在编译时应该知道的文字XQuery表达式。

You cannot pass them from the arguments or your functions or as variables, you can only hardcode them into the query. 您不能通过参数或函数传递它们,也不能将它们作为变量传递,只能将它们硬编码到查询中。

Treat them as reserved words (like SELECT or UPDATE ), which for some reason should be enclosed into single quotes. 将它们视为保留字(例如SELECTUPDATE ),出于某种原因,应将其括在单引号中。

You should build the whole query dynamically if you want the XQuery to be dynamic. 如果希望XQuery是动态的,则应动态构建整个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM