【问题标题】:SQL query with XML parameter带有 XML 参数的 SQL 查询
【发布时间】:2015-09-18 12:49:04
【问题描述】:

编辑:我在这里找到了堆栈溢出的相关答案: XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

我以前没有在 T-SQL 中处理过 XML,我正在修改一个现有的遗留存储过程,如果它通过反复试验得到了大部分。

但是我遇到了一个问题,即反复试验被证明是徒劳的,而且非常缓慢。认为是时候求助于堆栈溢出专家了!

这是一些 XML

000135141用户通知> 00135142用户通知> 用户通知> 通知>

有问题的存储过程接受上述 XML 作为参数:

创建过程 [dbo].[Notification_Insert] @ParametersXml XML 作为 开始

XML 包含子“UserNotification”元素。我想将每个UserNotification的UserId,MessageTypeId选择到这样的表中

用户名 |消息类型 ID 13514 | 1 13514 | 2

显然集合的大小是不固定的。

我目前的尝试(不起作用 - 沿着这些思路:

DECLARE @UserDetails TABLE (UserId INT, MessageTypeId INT); 插入@UserDetails (UserId, MessageTypeId) SELECT Tab.Col.value('@UserId','INT'), Tab.Col.value('@MessageTypeId','INT') FROM @ParametersXml.nodes('/Notification/UserNotifications[not(@xsi:nil = "true")][1]/UserNotification') AS Tab(Col)

但这永远不会插入任何东西..

我玩这个已经有一段时间了,没有任何乐趣:(

【问题讨论】:

    标签: tsql xquery-sql


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    我通过进一步搜索堆栈溢出找到了解决此问题的方法。

    我需要的查询(感谢XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

    插入@UserDetails (UserId, MessageTypeId) SELECT UserNotification.value('UserId[1]','INT'), UserNotification.value('MessageTypeId[1]','INT') FROM @ParametersXml.nodes('//Notification/UserNotifications') AS x(Col) 交叉应用 @ParametersXml.nodes('//Notification/UserNotifications/UserNotification') 作为 un(UserNotification)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-29
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多