EDIT: I have found a relevant answer already on stack overflow here: XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
I have not dealt with XML in T-SQL before, and I am modifying an existing legacy stored proc, and picking most if it up through trial and error.
however I have hit a problem where trial and error is proving fruitless, and very slow. Think it's time to appeal to stack overflow gurus!
Here is some XML
<?xml version=\"1.0\"?> <Notification xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <NotificationId>0</NotificationId> <UserNotifications> <UserNotification> <UserNotificationId>0</UserNotificationId> <NotificationId>0</NotificationId> <UserId>13514</UserId> <MessageTypeId>1</MessageTypeId> </UserNotification> <UserNotification> <UserNotificationId>0</UserNotificationId> <NotificationId>0</NotificationId> <UserId>13514</UserId> <MessageTypeId>2</MessageTypeId> </UserNotification> </UserNotifications> </Notification>
The Stored Proc in question accepts the above XML as a parameter:
CREATE PROCEDURE [dbo].[Notification_Insert] @ParametersXml XML AS BEGIN
The XML contains child "UserNotification" elements. I would like to select the UserId, MessageTypeId of each UserNotification, into a table like this
UserId | MessageTypeId 13514 | 1 13514 | 2
Obviously the size of the collection is not fixed.
My current attempt (which doesn't work - is along these lines:
DECLARE @UserDetails TABLE ( UserId INT, MessageTypeId INT); INSERT INTO @UserDetails (UserId, MessageTypeId) SELECT Tab.Col.value('@UserId','INT'), Tab.Col.value('@MessageTypeId','INT') FROM @ParametersXml.nodes('/Notification/UserNotifications[not(@xsi:nil = "true")][1]/UserNotification') AS Tab(Col)
But this never inserts anything..
I have been playing around with this for a while now and not had any joy :(