简体   繁体   中英

SQL Server XML queries

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>John.Doe@gmail.com</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.

Please try the following solution.

It is using MERGE statement to handle INSERT or UPDATE based on the email.

SQL

-- DDL and sample data population, start
DECLARE @Buyer TABLE (
    ID INT IDENTITY PRIMARY KEY, 
    pEmail NVARCHAR(100), 
    pName nvarchar(20), 
    pSurename nvarchar(20), 
    pCity nvarchar(20), 
    pCountry nvarchar(20)
);
INSERT INTO @Buyer (pEmail, pName, pSurename, pCity, pCountry) VALUES
(N'John.Doe@gmail.com', N'John', N'Doe', N'Madrid', N'Spain');
-- DDL and sample data population, end

-- before
SELECT * FROM @Buyer;


DECLARE @var XML =
N'<Buyer N="Johnny" S="Doe" CTY="Barcelona" CTR="Spain">
    <E>John.Doe@gmail.com</E> 
    <T>123456789</T> 
</Buyer>';

MERGE INTO @Buyer as Trg  
USING (
   SELECT c.value('(E/text())[1]', 'nvarchar(100)') as pEmail
        , c.value('@N', 'nvarchar(20)') as pName
        , c.value('@S', 'nvarchar(20)') as pSurename
        , c.value('@CTY', 'nvarchar(20)') as pCity
        , c.value('@CTR', 'nvarchar(20)') as pCountry
    FROM @var.nodes('/Buyer') AS t(c)
) as Src  
ON Trg.pEmail = Src.pEmail  
WHEN Matched /*AND Src.pName IS NOT NULL*/ THEN -- if needed to add additional conditions
UPDATE 
SET Trg.pName = Src.pName  
    , Trg.pSurename = Src.pSurename  
    , Trg.pCity = Src.pCity  
    , Trg.pCountry = Src.pCountry    
WHEN NOT MATCHED THEN  
INSERT (pEmail, pName, pSurename, pCity, pCountry) VALUES 
    (Src.pEmail, Src.pName, Src.pSurename, Src.pCity, Src.pCountry)
OUTPUT
    $action ,
    inserted.*;

-- after
SELECT * FROM @Buyer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM