//This below post will depict how you can insert the XML in the SQL table columns, how do you update the values of the Table reading the same XML
--Script for unified locations
DECLARE @LocationXml XML
--Assume that this is the Input XML.
SET @LocationXml = '
--Assume this is the existing Datbase with locations
DECLARE @Exbname TABLE(
cLocations nvarchar(max),
cCountryName nvarchar(MAX),
cID int
)
--Assume this is the existing Location inserted in the DB
INSERT INTO @Exbname VALUES('a','c',0)
--Assume this is the new Locations given by the supplier in the same format.
DECLARE @tbname TABLE(
cLocations nvarchar(max),
cCountryName nvarchar(MAX),
cID int
)
--Assume this is the new Locations cached in a temp table. (so that we decied to either insert or update them)
INSERT INTO @tbname
SELECT LocationXml.snx.value('(.)[1]', 'NVARCHAR(50)') AS 'cLocations',
LocationXml.snx.value('(./@Country)[1]', 'NVARCHAR(50)') AS 'cCountryName',
LocationXml.snx.value('(./@ID)[1]', 'NVARCHAR(50)') AS 'cID'
FROM @LocationXml.nodes('//Locations/Location') LocationXml ( snx )
--SELECT * FROM @tbname --WHERE [@tbname].cID = 3
--This is how we will update the existing loations
UPDATE extb
SET extb.cLocations = newtb.cLocations,
extb.cCountryName = newtb.cCountryName
FROM @tbname newtb INNER JOIN @Exbname extb ON newtb.cID = extb.cID
-- This is how we insert the new locations
INSERT INTO @Exbname
SELECT * FROM @tbname WHERE cID NOT IN (
SELECT cID FROM @Exbname
)
SELECT * FROM @Exbname