Sorry if the topic isn't very clear, but here is what I need to accomplish. I have a table like so:
user_id | friends
==============================
75 | <friend name="bob" /><friend name="joe" />
76 | <friend name="bill" /><friend name="bob" />
77 | <friend name="sam" /><friend name="gary" /><friend name="john" />
I need to get the number of friends (ie, number of XML nodes) for each user. The resulting table should look like this:
user_id | number_of_friends
==============================
75 | 2
76 | 2
77 | 3
The following SQL can do it for one column at a time, but I'm not sure how to do it for all columns in one go.
DECLARE @x XML;
SELECT @x = pval
FROM [mytable]
WHERE uid=75 AND pkey='roster';
SELECT COUNT(t.c.value('@name', 'NVARCHAR(MAX)')) AS number_of_friends
FROM @x.nodes('//friend') t(c);
This results in the table:
number_of_friends
=================
2