Try using a table-valued function.
CREATE FUNCTION dbo.GetTableFromXML(@xml XML) RETURNS @retXMLTable TABLE ( -- Columns returned by the function ID int PRIMARY KEY NOT NULL, Name nvarchar(max) NULL, LName nvarchar(max) NULL, )AS BEGIN INSERT @retXMLTable (ID,FirstName,LName) select @xml.value('/Employees[1]/Person[1]/ID[1]', 'nvarchar(max)'), @xml.value('/Employees[1]/Person[1]/Name[1]', 'nvarchar(max)') @xml.value('/Employees[1]/Person[1]/LName [1]', 'nvarchar(max)') RETURN; END;
In general, the same as the answer from Oleg, but you can work with the result in the form of a table. If you recreate the sample, you will immediately receive all your entries from the xml table.
source share