I'm trying to find a solution for this task: write a procedure that accepts XML document in the following format:
<Buyer N="John" S="Doe" CTY="Madrid" CTR="Spain">
<E>[email protected]</E>
<T>123456789</T>
</Buyer>
The procedure must verify if your database already contains the country, city or buyer (check buyer by email).
If any data is missing in your database, your procedure has to insert it into the table.
Call the functions that show the mentioned procedure functionalities.
This is how I started to separate all the important things:
CREATE PROCEDURE pXML
@var XML
AS
SELECT
X.U.value('@N', 'nvarchar(20)') AS pName,
X.U.value('@S', 'nvarchar(20)') AS pSurename,
X.U.value('@CTY', 'nvarchar(20)') AS pCity,
X.U.value('@CTR', 'nvarchar(20)') AS pCountry
FROM
@var.nodes('/Buyer') AS X(U)
SELECT X.U.value('.', 'nvarchar(50)' ) AS pEmail
FROM @var.nodes('/Buyer/E') as X(U)
What I don't know is how to put IF statement, or whatever condition is necessary INSIDE that procedure.
My pseudo code would be:
declare @temp nvarchar(30)
set @temp = pEmail
IF NOT EXISTS(select * from Buyers where Buyer.Email = pEmail)
INSERT INTO Buyer values (pName, pSurename, pCity, pCountry)
(more IF NOT EXISTS statements for city and for country)
Is anyone willing to give me a hand here?
Thanks in advance! Tell me if didn't clarify enough, I'll do my best to add any needed info.