【问题标题】:Syntax to read xml inside of a stored procedure在存储过程中读取 xml 的语法
【发布时间】:2020-06-01 19:59:21
【问题描述】:

我正在向@xCriteria 传递一个如下所示的 XML 参数:

<?xml version="1.0" encoding="utf-8"?>
<searchParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <includeInactive enabled="True" />
</searchParameters>

我知道 xpath 是://includeInactive/@enabled

并尝试过:

--determine if we include inactive users
DECLARE @isInactive AS bit

-- SELECT @isInactive = @xCriteria.value('(/includeInactive/@enabled', 'bit')
-- SET @isInactive = @xCriteria.query('/includeInactive/@enabled')
-- SELECT @isInactive.value('(/includeInactive/enabled)[1]', 'bit')
-- SELECT @isInactive = @xCriteria.query('/includeInactive/@enabled')  

PRINT '@isInactive'
PRINT @isInactive

如何获取布尔值?

【问题讨论】:

  • 在 Microsoft SQL Server 中

标签: sql stored-procedures sql-server-2012


【解决方案1】:

你可以这样尝试:

DECLARE @isInactive AS BIT;

SELECT
    @isInactive = CASE @param.value('(/searchParameters/includeInactive)[1]/@enabled', 'bit') 
                     WHEN 1 THEN 0 ELSE 1
                  END

SELECT @isInactive;

您基本上需要选择根节点内的第一个((....)[1]&lt;includeInactive&gt; 节点,然后从该节点获取@enabled 属性。

您需要笨拙的CASE 表达式,因为您的 XML 存储了 enabled 的值,而您想要获取的变量正好相反 - @isInactive .

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    相关资源
    最近更新 更多