This is a portion of a much larger stored procedure, but before I can get going I have to first parse XML, extract the attributes, and insert them into a temp table. Then I will use that table to perform functions later on.
When I try to insert into the temp table I am getting "Subquery returned more than 1 value". If my dummy XML contains one node it works fine, but if I add additional nodes issues come up.
Full SQL including XML to test:
DECLARE @MESSAGELIST XML
set @MESSAGELIST =
'<object>
<Record FirstName = ''Red'' LastName = ''Shark'' Email = ''D@d.com'' Date = ''01/01/2001'' ></Record>
<Record FirstName = ''Jon'' LastName = ''Slow'' Email = ''D@d.com'' Date = ''01/01/2011'' ></Record>
<Record FirstName = ''Tyrone'' LastName = ''Lennystar'' Email = ''D@d33.com'' Date = ''01/11/2011'' ></Record>
</object>'
IF (OBJECT_ID('tempdb..#NHOMessagesTemp') IS NOT NULL)
DROP TABLE #NHOMessagesTemp
CREATE TABLE #NHOMessagesTemp
(
FirstName nvarchar(50),
LastName nvarchar(50),
Email nvarchar(100),
MessageDate datetime
)
INSERT INTO #NHOMessagesTemp
VALUES (
(SELECT I.FirstName.value('@FirstName', 'nvarchar(50)')
FROM @MESSAGELIST.nodes('/object/Record') AS I(FirstName)),
(SELECT I.LastName.value('@LastName', 'nvarchar(50)')
FROM @MESSAGELIST.nodes('/object/Record') AS I(LastName)),
(SELECT I.Email.value('@Email', 'nvarchar(100)')
FROM @MESSAGELIST.nodes('/object/Record') AS I(Email)),
(SELECT I.[Date].value('@Date', 'datetime')
FROM @MESSAGELIST.nodes('/object/Record') AS I([Date]))
)
SELECT *
FROM #NHOMessagesTemp
I am sure the problem is with my
FROM @MESSAGELIST.nodes('/object/Record') AS...
but I don't know how to loop through the XML and get all of the values.
Thank you for your help!