This is the query I am currently using:
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
Where my audit table contains a parameters XML node that looks something like this:
UserName
ComputerName
What I am wondering if is there is a way to get rid of the redundant parameters.value('(params/param[position()=sql:variable("@Index")] section and maybe do it as a join?
You can simplify a bit by using
cross apply.