在存储过程中读取 xml 的语法

Syntax to read xml inside of a stored procedure

我正在向 @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

如何获取布尔值?

你这样试试:

DECLARE @isInactive AS BIT;

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

SELECT @isInactive;

您基本上需要 select 根节点内的第一个 ((....)[1]) <includeInactive> 节点,然后从该节点获取 @enabled 属性。

并且你需要笨拙的 CASE 表达式,因为你的 XML 存储了 enabled 的值,而你想要获取的变量是准确的相反 - @isInactive.