Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
van toepassing op:SQL Server
Azure SQL Database
Azure SQL Managed Instance
SQL-database in Microsoft Fabric
Voegt één of meer knooppunten in die door Expression1 zijn geïdentificeerd als kindknopen of broers of zussen van de door Expression2 geïdentificeerde knoop.
Syntaxis
insert Expression1 (
{as first | as last} into | after | before
Expression2
)
Arguments
Expressie1
Identificeert één of meer knooppunten om in te voegen. Dit kan een constante XML-instantie zijn; een verwijzing naar een getypte XML-datatype-instantie van dezelfde XML Schema-collectie waarop de modificatiemethode wordt toegepast; een niet-getypte XML-datatype-instantie met een zelfstandige sql:column()/sql:variable() -functie; of een XQuery-expressie. De expressie kan resulteren in een knoop, en ook in een tekstknoop, of in een geordende reeks knopen. Het kan niet worden opgelost naar de root (/) knoop. Als de expressie resulteert in een waarde of een reeks waarden, worden de waarden ingevoegd als één tekstknoop met een ruimte tussen elke waarde in de reeks. Als je meerdere knopen als constant specificeert, worden de knopen tussen haakjes geplaatst en gescheiden door komma's. Je kunt heterogene reeksen niet invoegen, zoals een reeks elementen, attributen of waarden. Als Expression1 oplost naar een lege sequentie, vindt er geen insertie plaats en worden er geen fouten teruggegeven.
in
Knooppunten die door Expression1 zijn geïdentificeerd, worden ingevoegd als directe afstammelingen (kindknopen) van de knoop die door Expression2 is geïdentificeerd. Als de node in Expression2 al één of meer kindnodes heeft, moet je ofwel als eerste of als laatste gebruiken om aan te geven waar je de nieuwe node wilt toevoegen. Bijvoorbeeld aan het begin of aan het einde van de kindlijst, respectievelijk. De als eerste en als laatste trefwoorden worden genegeerd wanneer attributen worden ingevoegd.
after
Knooppunten die door Expression1 zijn geïdentificeerd, worden direct na de door Expression2 geïdentificeerde knoop als broers en zussen ingevoegd. Het after keyword kan niet worden gebruikt om attributen in te voegen. Het kan bijvoorbeeld niet worden gebruikt om een attribuutconstructor in te voegen of om een attribuut van een XQuery terug te geven.
before
Knooppunten die door Expression1 zijn geïdentificeerd, worden als broers en zussen ingevoegd direct vóór de door Expression2 geïdentificeerde knoop. Het voor-sleutelwoord kan niet worden gebruikt wanneer attributen worden ingevoegd. Het kan bijvoorbeeld niet worden gebruikt om een attribuutconstructor in te voegen of om een attribuut van een XQuery terug te geven.
Expressie2
Identificeert een knooppunt. De knooppunten die in Expression1 zijn geïdentificeerd, worden ingevoegd ten opzichte van de node die door Expression2 is geïdentificeerd. Dit kan een XQuery-expressie zijn die een referentie teruggeeft naar een knoop die bestaat in het momenteel gerefereerde document. Als meer dan één knoop wordt teruggegeven, faalt de insert. Als Expression2 een lege reeks teruggeeft, vindt er geen insertie plaats en worden er geen fouten teruggegeven. Als Expression2 statisch geen singleton is, wordt een statische fout teruggegeven.
Expression2 kan geen verwerkingsinstructie, opmerking of attribuut zijn. Let op dat Expression2 een verwijzing moet zijn naar een bestaande node in het document en niet naar een geconstrueerde node.
Voorbeelden
Eén. Elementenknopen invoegen in het document
Het volgende voorbeeld illustreert hoe je elementen in een document kunt invoegen. Ten eerste wordt een XML-document toegewezen aan een variabele van het xml-type. Vervolgens illustreert het voorbeeld via verschillende insert XML DML-statements hoe elementknooppunten in het document worden ingevoegd. Na elke invoeging toont de SELECT-instructie het resultaat.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
</Features>
</ProductDescription>
</Root>' ;
SELECT @myDoc;
-- insert first feature child (no need to specify as first or as last)
SET @myDoc.modify('
insert <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
into (/Root/ProductDescription/Features)[1]') ;
SELECT @myDoc ;
-- insert second feature. We want this to be the first in sequence so use 'as first'
SET @myDoc.modify('
insert <Warranty>1 year parts and labor</Warranty>
as first
into (/Root/ProductDescription/Features)[1]
') ;
SELECT @myDoc ;
-- insert third feature child. This one is the last child of <Features> so use 'as last'
SELECT @myDoc
SET @myDoc.modify('
insert <Material>Aluminum</Material>
as last
into (/Root/ProductDescription/Features)[1]
')
SELECT @myDoc ;
-- Add fourth feature - this time as a sibling (and not a child)
-- 'after' keyword is used (instead of as first or as last child)
SELECT @myDoc ;
SET @myDoc.modify('
insert <BikeFrame>Strong long lasting</BikeFrame>
after (/Root/ProductDescription/Features/Material)[1]
') ;
SELECT @myDoc;
GO
Let op dat verschillende padexpressies in dit voorbeeld "[1]" specificeren als een vereiste voor statische typering. Dit zorgt voor één doelknoop.
B. Meerdere elementen in het document invoegen
In het volgende voorbeeld wordt een document eerst toegewezen aan een variabele van het xml-type . Vervolgens wordt een reeks van twee elementen, die productkenmerken vertegenwoordigen, toegewezen aan een tweede variabele van xml-type . Deze reeks wordt vervolgens in de eerste variabele ingevoegd.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc = N'<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features> </Features>
</ProductDescription>
</Root>';
DECLARE @newFeatures xml;
SET @newFeatures = N'<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>';
-- insert new features from specified variable
SET @myDoc.modify('
insert sql:variable("@newFeatures")
into (/Root/ProductDescription/Features)[1] ')
SELECT @myDoc;
GO
C. Attributen invoegen in een document
Het volgende voorbeeld illustreert hoe attributen in een document worden ingevoegd. Ten eerste wordt een document toegewezen aan een xml-typevariabele. Vervolgens wordt een reeks insert XML DML-instructies gebruikt om attributen in het document in te voegen. Na elke attribuutinvoeging toont de SELECT-instructie het resultaat.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<Location LocationID="10" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>' ;
SELECT @myDoc;
-- insert LaborHours attribute
SET @myDoc.modify('
insert attribute LaborHours {".5" }
into (/Root/Location[@LocationID=10])[1] ');
SELECT @myDoc;
-- insert MachineHours attribute but its value is retrieved from a sql variable @Hrs
DECLARE @Hrs FLOAT;
SET @Hrs =.2;
SET @myDoc.modify('
insert attribute MachineHours {sql:variable("@Hrs") }
into (/Root/Location[@LocationID=10])[1] ');
SELECT @myDoc;
-- insert sequence of attribute nodes (note the use of ',' and ()
-- around the attributes.
SET @myDoc.modify('
insert (
attribute SetupHours {".5" },
attribute SomeOtherAtt {".2"}
)
into (/Root/Location[@LocationID=10])[1] ');
SELECT @myDoc;
GO
D. Een commentaarsknoop invoegen
In deze query wordt eerst een XML-document toegewezen aan een variabele van het type xml . Vervolgens wordt XML DML gebruikt om een commentaarknoop in te voegen na het eerste <step> element.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<Location LocationID="10" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>' ;
SELECT @myDoc;
SET @myDoc.modify('
insert <!-- some comment -->
after (/Root/Location[@LocationID=10]/step[1])[1] ');
SELECT @myDoc;
GO
E. Een verwerkingsinstructie invoegen
In de volgende query wordt eerst een XML-document toegewezen aan een variabele van het type xml . Vervolgens wordt het XML DML-sleutelwoord gebruikt om een verwerkingsinstructie in te voegen aan het begin van het document.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<Location LocationID="10" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>' ;
SELECT @myDoc ;
SET @myDoc.modify('
insert <?Program = "Instructions.exe" ?>
before (/Root)[1] ') ;
SELECT @myDoc ;
GO
F. Gegevens invoegen met behulp van een CDATA-sectie
Wanneer je tekst invoegt met tekens die niet geldig zijn in XML, zoals < of >, kun je CDATA-secties gebruiken om de gegevens in te voegen zoals getoond in de volgende query. De query specificeert een CDATA-sectie, maar deze wordt toegevoegd als een tekstknoop waarbij eventuele ongeldige tekens worden omgezet in entiteiten. Bijvoorbeeld, < wordt opgeslagen als <.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features> </Features>
</ProductDescription>
</Root>' ;
SELECT @myDoc ;
SET @myDoc.modify('
insert <![CDATA[ <notxml> as text </notxml> or cdata ]]>
into (/Root/ProductDescription/Features)[1] ') ;
SELECT @myDoc ;
GO
De query voegt een tekstknoop in het <Features> element in:
<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features> <notxml@gt; as text </notxml> or cdata </Features>
</ProductDescription>
</Root>
G. Tekstknoop invoegen
In deze query wordt eerst een XML-document toegewezen aan een variabele van het type xml . Vervolgens wordt XML DML gebruikt om een tekstknoop als eerste kind van het <Root> element in te voegen. De tekstconstructor wordt gebruikt om de tekst te specificeren.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
</Features>
</ProductDescription>
</Root>'
SELECT @myDoc;
SET @myDoc.modify('
insert text{"Product Catalog Description"}
as first into (/Root)[1]
');
SELECT @myDoc;
H. Een nieuw element invoegen in een niet-getypeerde xml-kolom
Het volgende voorbeeld past XML DML toe om een XML-instantie bij te werken die is opgeslagen in een xml-type kolom:
USE AdventureWorks;
GO
CREATE TABLE T (i INT, x XML);
GO
INSERT INTO T VALUES(1,'<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>');
GO
-- insert a new element
UPDATE T
SET x.modify('insert <Material>Aluminum</Material> as first
into (/Root/ProductDescription/Features)[1]
');
GO
Ook hier moet de padexpressie bij het invoegen van de <Material> elementknoop één doel teruggeven. Dit wordt expliciet gespecificeerd door een [1] aan het einde van de expressie toe te voegen.
-- check the update
SELECT x.query(' //ProductDescription/Features')
FROM T;
GO
I. Invoegen op basis van een if-voorwaarde
In het volgende voorbeeld wordt een IF-voorwaarde gespecificeerd als onderdeel van Expression1 in de insert XML DML-instructie. Als de voorwaarde True is, wordt er een attribuut aan het <WorkCenter> element toegevoegd.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<Location LocationID="10" LaborHours="1.2" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';
SELECT @myDoc
SET @myDoc.modify('
insert
if (/Root/Location[@LocationID=10])
then attribute MachineHours {".5"}
else ()
as first into (/Root/Location[@LocationID=10])[1] ');
SELECT @myDoc;
GO
Het volgende voorbeeld is vergelijkbaar, behalve dat de insert XML DML-instructie een element in het document invoegt als de voorwaarde True is. Dat wil zeggen, als het <WorkCenter> element kleiner is dan of gelijk is aan twee <step> kindelementen.
USE AdventureWorks;
GO
DECLARE @myDoc XML;
SET @myDoc =
'<Root>
<Location LocationID="10" LaborHours="1.2" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';
SELECT @myDoc;
SET @myDoc.modify('
insert
if (count(/Root/Location/step) <= 2)
then element step { "This is a new step" }
else ()
as last into (/Root/Location[@LocationID=10])[1] ');
SELECT @myDoc;
GO
Dit is het resultaat:
<Root>
<WorkCenter WorkCenterID="10" LaborHours="1.2">
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
<step>This is a new step</step>
</WorkCenter>
J. Knopen invoegen in een getypeerde xml-kolom
Dit voorbeeld voegt een element en een attribuut in een manufacturing instructions XML die is opgeslagen in een getypeerde xml-kolom .
In het voorbeeld maak je eerst een tabel (T) aan met een getypte xml-kolom , in de AdventureWorks-database. Vervolgens kopieer je een manufacturing instructions XML-instantie uit de kolom Instructions in de ProductModel-tabel naar tabel T. Inserties worden vervolgens toegepast op XML in tabel T.
USE AdventureWorks;
GO
DROP TABLE T;
GO
CREATE TABLE T(
ProductModelID INT PRIMARY KEY,
Instructions XML (Production.ManuInstructionsSchemaCollection));
GO
INSERT T
SELECT ProductModelID, Instructions
FROM Production.ProductModel
WHERE ProductModelID=7;
GO
SELECT Instructions
FROM T;
-- now insertion begins
--1) insert a new manu. Location. The <Root> specified as
-- expression 2 in the insert() must be singleton.
UPDATE T
SET Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
insert <MI:Location LocationID="1000" >
<MI:step>New instructions go here</MI:step>
</MI:Location>
as first
into (/MI:root)[1]
') ;
SELECT Instructions
FROM T ;
-- 2) insert attributes in the new <Location>
UPDATE T
SET Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
insert attribute LaborHours { "1000" }
into (/MI:root/MI:Location[@LocationID=1000])[1] ');
GO
SELECT Instructions
FROM T ;
GO
--cleanup
DROP TABLE T ;
GO
Zie ook
Vergelijk getypte XML met niet-getypte XML
Exemplaren van XML-gegevens maken
xml-gegevenstypemethoden
Taal voor wijziging van XML-gegevens (XML DML)