I am trying to insert data into a temporary table from a xml file, problem is that I can enter the data one row at a time from this:
DECLARE @tblCountry TABLE
(
CountryCode nvarchar(5),
CountryName nvarchar(50)
)
INSERT INTO @tblCountry(CountryCode, CountryName)
SELECT --DISTINCT
x.v.value('(/countries/country/@code)[1]','VARCHAR(5)'),
x.v.value('(/countries/country/text())[1]','VARCHAR(50)')
FROM @countryXML.nodes('/countries/country') x(v)
SELECT CountryCode, CountryName FROM @tblCountry
but when I am trying to put it in a loop I can not get it to work:
create proc spcountry
@count int
AS
BEGIN
DECLARE @countryXML XML
SET @countryXML = '<countries>
<country code="AF" iso="4">Afghanistan</country>
<country code="ZW" iso="716">Zimbabwe</country>
</countries>'
DECLARE @tblCountry TABLE
(
CountryCode nvarchar(5),
CountryName nvarchar(50)
)
SET @count = 1
WHILE (@count < 238)
BEGIN
INSERT INTO @tblCountry(CountryCode, CountryName)
SELECT --DISTINCT
x.v.value('(/countries/country/@code)[sql:variable("@count")]','VARCHAR(5)'),
x.v.value('(/countries/country/text())[[sql:variable("@count")]]','VARCHAR(50)')
FROM @countryXML.nodes('/countries/country') x(v)
set @count = @count + 1
END
END
SELECT CountryCode, CountryName FROM @tblCountry
I was following this link's solution: XML data type method “value” must be a string literal