SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XMLQuery on NVARCHAR MAX column using filter


XMLQuery on NVARCHAR MAX column using filter

Author
Message
SATISD9X
SATISD9X
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1022 Visits: 180
I've a table with ID & MSG columns. ID is unique identifier & msg is NVARCHAR MAX.

abc><poc><tna name="a" value="abc" /><tna name="b" value="def" /><tna name="c" value="xyz" /></poc></abc>

I need to find the value "def" from <tna> tag whose Name = "b" based on the ID [where clause].

This is a huge table. Need to consider the performance as well.
Tavis Reddick
Tavis Reddick
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 191
I think you can do this with the value function using XPath in SQL Server 2005 and later, although here I've used a Common Table Expression from 2008 to convert the NVARCHAR(MAX) to XML as an intermediary step:
-- Set up table variable
DECLARE @test_XMLQuery1 TABLE (
ID INT PRIMARY KEY,
MSG NVARCHAR(MAX) NOT NULL);
-- Insert some test values
INSERT INTO @test_XMLQuery1
VALUES (1, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def-not" /><tna name="c" value="xyz" /></poc></abc>')
, (2, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def" /><tna name="c" value="xyz" /></poc></abc>')
, (3, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def-neither" /><tna name="c" value="xyz" /></poc></abc>')
-- Select value attribute of the appropriate tna element for the selected row
WITH test_XMLQuery2 AS (
SELECT ID, CAST(MSG AS XML) AS MSGXML
FROM @test_XMLQuery1
WHERE ID = 2
)
SELECT ID, MSGXML.value('(abc/poc/tna[@name = "b"]/@value)[1]', 'NVARCHAR(50)') AS value
FROM test_XMLQuery2


I cannot give you any guidance on performance, though.
Eirikur Eiriksson
Eirikur Eiriksson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14956 Visits: 18591
Using Tavis's data sample, here is a slightly more efficient query
Cool

DECLARE @ID INT = 2;
-- Set up table variable
DECLARE @test_XMLQuery1 TABLE (
ID INT PRIMARY KEY,
MSG NVARCHAR(MAX) NOT NULL);
-- Insert some test values
INSERT INTO @test_XMLQuery1
VALUES (1, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def-not" /><tna name="c" value="xyz" /></poc></abc>')
, (2, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def" /><tna name="c" value="xyz" /></poc></abc>')
, (3, '<abc><poc><tna name="a" value="abc" /><tna name="b" value="def-neither" /><tna name="c" value="xyz" /></poc></abc>')
;WITH test_XMLQuery2 AS (
SELECT ID, CAST(MSG AS XML) AS MSGXML
FROM @test_XMLQuery1
WHERE ID = @ID
)
SELECT
ID
,B.N.value('@value','VARCHAR(10)') AS value
FROM test_XMLQuery2 X
OUTER APPLY X.MSGXML.nodes('abc/poc/tna[@name = "b"]') AS B(N);



Edit: missing code section
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search