【问题标题】:The argument 1 of the XML data type method "nodes" must be a string literalXML 数据类型方法“nodes”的参数 1 必须是字符串字面量
【发布时间】:2014-02-05 01:11:01
【问题描述】:

我在其他地方尝试了一段 sql 查询来从 XML 字符串中获取值并且它可以工作,但是当我尝试在 几乎类似的情况下在其他地方实现它时,我收到了这个错误:

XML 数据类型方法节点的参数 1 必须是字符串字面量

我的查询是这样的:

        Select * into #ContentItemsOnStaging from ( SELECT A.id as
        NodeID,A.text as NodeName,B.text as Parent , A.createDate as
        DateCreated ,
-- Problem is from here
        ( SELECT pref.value('(/' + E.alias
        +'/@updateDate)[1]', 'datetime') AS LastUpdatedDate FROM   
(SELECT  CAST(xml AS xml) AS XmlCol FROM [ContentXml] WHERE nodeId = A.id )  
AS XmlStuff CROSS APPLY XmlCol.nodes('/' + E.alias) AS x(pref) ) 
-- To Here
        FROM [Node] as A left join Node B on A.parentID = B.id left join [ContentXml] as C on 
    A.id = C.nodeId  left join [Content] as D on A.id = D.nodeId  left join [cmsContentType] 
    as E on D.contentType = E.nodeId) as test2

问题来自 E.Alias。 我需要的是发送一个可自定义的 Header 以供搜索。有什么解决方法吗?

有什么想法吗?

【问题讨论】:

    标签: sql-server xml


    【解决方案1】:

    您正在尝试将cmsContentType.Alias 中的关系数据绑定到xml 元素名称。错误的原因是您正在通过字符串连接构建 xquery 路径,这不起作用。

    要从您的 xquery 中引用该列,您需要使用 sql:column() 函数。下面是一个简单的例子,more info here.

    declare @tab table (Alias varchar(100));
    insert into @tab
        select 'one' union all
        select 'two'
    
    
    declare @x xml;
    set @x = '<root><one updateDate="01-01-2014" /><two updateDate="01-01-2013" /></root>';
    
    select  Alias,
            @x.value('(//*[local-name(.)=sql:column("t.Alias")])[1]/@updateDate', 'datetime')
    from    @tab t;
    

    这将为您提供&lt;root&gt; 下方的第一个元素,该元素与您的Alias 值连接,然后检索一个属性值 (updateDate)。

    如果这有帮助,请告诉我们。

    【讨论】:

    • 问题是在您的情况下,xml 字符串并不总是以特定标记(根)开头。标题取决于文档类型。这就是我试图通过 E.alias 的原因。
    • @amirmoradifard 查看更新,现在没有前导根路径。双斜杠 // 将搜索整个文档,这不是很好,但如果您没有已知的 xpath 可能是您唯一的选择
    • +1 by sql:column("alias").. 看起来很棒,现在我正在考虑如何探索在节点上动态工作的完整 xml 模式 :)
    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2018-11-18
    • 2022-08-21
    • 2023-02-14
    • 2021-06-02
    相关资源
    最近更新 更多