Write for Us
Example:1)declare @x xmldeclare @custid intset @x='<root><CustomerDescription CustID="101" CustomerName="ABCL Industries Limited"><Phonenumber><Work>1234567890</Work><Residence>1434546678</Residence></Phonenumber></CustomerDescription><CustomerDescription CustID="102" CustomerName="HAL Industries Limited"><Phonenumber><Work>1234567890</Work><Residence>1434546678</Residence></Phonenumber></CustomerDescription></root>'select @xset @x.modify('delete /root/CustomerDescription/@CustID')select @xResult:<root><CustomerDescription CustomerName="ABCL Industries Limited"><Phonenumber><Work>1234567890</Work><Residence>1434546678</Residence></Phonenumber></CustomerDescription><CustomerDescription CustomerName="HAL Industries Limited"><Phonenumber><Work>1234567890</Work><Residence>1434546678</Residence></Phonenumber></CustomerDescription></root>2)To insert a new node or fragment of XML,we can use the INSERT statement.The syntax for it is as follows:Syntax:insert Expression1 ({as first | as last} into | after | beforeExpression2)Arguments:Expression1 Identifies one or more nodes to insert. into Nodes identified by Expression1 are inserted as direct descendents (child nodes) of the node identified by Expression2. If the node in Expression2 already has one or more child nodes, the user must use either as first or as last to specify where the new node has to be added. For example, at the start or at the end of the child list, respectively. after Nodes identified by Expression1 are inserted as siblings directly after the node identified by Expression2. before Nodes identified by Expression1 are inserted as siblings directly before the node identified by Expression2. Expression2 Identifies a node. The nodes identified in Expression1 are inserted relative to the node identified by Expression2.Example:USE AdventureWorks;GODECLARE @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
Result:<Root><ProductDescription ProductID="1" ProductName="Road Bike"><Features><Maintenance>3 year parts and labor extended maintenance is available</Maintenance></Features></ProductDescription></Root> For more examples,refer to” insert(XML DML)” in SQL Server 2005 books online.5) Nodes MethodThe nodes method can be used to extract data from an XML document and use that to generate subnodes that can be used for various purposes,such as,to create new content or insert content into new tables.Example:declare @x xmlset @x='<Root><row id="1"><AirportCode>MUM</AirportCode><AirportName>Mumbai</AirportName></row><row id="2"><AirportCode>MAS</AirportCode><AirportName>Madras</AirportName></row><row id="3"></row></Root>'
Select T.c.query('.') as resultfrom @x.nodes('/Root/row') T(c) << Prev Page Next Page>>