这是我目前使用的查询:
DECLARE @Index AS INT
SET @Index = 1
SELECT
auditId,
parameters.value('(params/param[position()=sql:variable("@Index")]/node())[1]', 'nvarchar(256)') AS value,
parameters.value('(params/param[position()=sql:variable("@Index")]/@entityId)[1]', 'nvarchar(256)') AS entityId,
parameters.value('(params/param[position()=sql:variable("@Index")]/@entityType)[1]', 'bigint') AS entityType,
parameters.value('(params/param[position()=sql:variable("@Index")]/@localizable)[1]', 'bit') AS localizable
FROM
audit
我的审计表包含一个参数XML节点,如下所示: 用户名 计算机名
我想知道是否有办法摆脱多余的parameters.value('(params/param[position()=sql:variable("@Index")]
部分并且可能将其作为连接进行?
答案 0 :(得分:3)
您可以使用cross apply
简化一点。
SELECT
auditId,
T.N.value('.', 'nvarchar(256)') AS value,
T.N.value('@entityId', 'nvarchar(256)') AS entityId,
T.N.value('@entityType', 'bigint') AS entityType,
T.N.value('@localizable', 'bit') AS localizable
FROM audit
CROSS APPLY parameters.nodes('/params/param[sql:variable("@Index")]') AS T(N)