Zu diesem XML-Beispiel:
<Record xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
<Emp>
<ID>3</ID>
<EmpName>Dibyendu</EmpName>
<Sal>3500</Sal>
</Emp>
</Record>Und dieser Tabelle:
CREATE TABLE Employee (
[ID] [int] NOT NULL,
[EmpName] varchar(max) NOT NULL,
[Sal] [int] NULL
);Die folgende Prozedur nutzt die XPath-Methode value(), um das XML in Spalten zu zerlegen:
CREATE PROCEDURE AddEmployee @empXml xml
AS
INSERT INTO Employee (ID, EmpName, Sal)
VALUES (
@empXml.value('(/Record/Emp/ID)[1]', 'int'),
@empXml.value('(/Record/Emp/EmpName)[1]', 'varchar(max)'),
@empXml.value('(/Record/Emp/Sal)[1]', 'int')
);Dann führen Sie sie aus mit:
EXEC AddEmployee '<Record xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"><Emp><ID>3</ID><EmpName>Dibyendu</EmpName><Sal>3500</Sal></Emp></Record>';Wenn das Record-XML mehrere Emp-Elemente enthalten kann, sollten Sie das auf nodes() und CROSS APPLY umbauen.