

#Insert node basex how to#
The following example illustrates how to insert elements into a document. Inserting element nodes into the document Note that Expression2 must be a reference to an existing node in the document and not a constructed node. Expression2 cannot be a processing instruction, comment, or attribute. If Expression2 is statically not a singleton, a static error is returned. If Expression2 returns an empty sequence, no insertion occurs and no errors are returned. If more than one node is returned, the insert fails. This can be an XQuery expression that returns a reference to a node that exists in the currently referenced document. The nodes identified in Expression1 are inserted relative to the node identified by Expression2. For example, it cannot be used to insert an attribute constructor or to return an attribute from an XQuery. The before keyword cannot be used when attributes are being inserted. Nodes identified by Expression1 are inserted as siblings directly before the node identified by Expression2.

The after keyword cannot be used to insert attributes. Nodes identified by Expression1 are inserted as siblings directly after the node identified by Expression2. The as first and as last keywords are ignored when attributes are inserted. For example, at the start or at the end of the child list, respectively. If the node in Expression2 already has one or more child nodes, you must use either as first or as last to specify where you want the new node added. Nodes identified by Expression1 are inserted as direct descendents (child nodes) of the node identified by Expression2.

If Expression1 resolves to an empty sequence, no insertion occurs and no errors are returned. You cannot insert heterogeneous sequences such as a sequence of elements, attributes, or values. If you specify multiple nodes as constant, the nodes are included in parentheses and are separated by commas. If the expression results in a value or a sequence of values, the values are inserted as a single text node with a space separating each value in the sequence. The expression can result in a node, and also a text node, or in an ordered sequence of nodes. This can be a constant XML instance a reference to a typed XML data type instance of the same XML Schema collection on which the modify method is being applied an untyped XML data type instance using a stand-alone sql:column()/ sql:variable() function or an XQuery expression. To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
