declare @xml xml
set @xml='<Supplier><user userNo="1" email="gautam.singh@gmail.com"><item no="1" name="Item1"></item></user>
<user userNo="2" email="gautam1.singh@gmail.com"><item no="2" name="Item2"></item></user>
<user userNo="3" email="gautam2.singh@gmail.com"><item no="6" name="Item3"></item></user>
<user userNo="3" email="gautam2.singh@gmail.com"><item no="6" name="Item3"></item></user>
</Supplier>'
-----Select
--SELECT @xml.exist('/Supplier/user[@email = "gautam12.singh@gmail.com"]') as Result1
--SELECT @xml.query('/Supplier/user/item') as Users
-- --select Distincts
--SELECT @xml.query('distinct-values(data(/Supplier/user/item/@no))') as Items
----select on particular column
--SELECT @xml.value('/Supplier[1]/user[1]/@email', 'VARCHAR(50)') as ResultEmail1
--SELECT x.value('@userNo', 'int') AS UserNo, x.value('@email', 'varchar(50)') AS Email FROM @xml.nodes('/Supplier/user') TempXML (x)
-- --modify
--DECLARE @userNo int =120
--SET @xml.modify ('replace value of (/Supplier/user/@userNo)[3] with sql:variable("@userNo")')
--SELECT @xml;
--SET @xml.modify(' replace value of (/Supplier/user/@userNo)[3] with ( if (count(/Supplier/user[3]/item) > 1) then "3.0" else "1.0" ) ')
--SELECT @xml;
----Delete
--DECLARE @itemNo int=2
--SET @xml.modify(' delete Supplier/user/item[@no=sql:variable("@itemNo")]')
--SELECT @xml;
No comments:
Post a Comment