sqlxml

Count the number of sub tags from XML in SQL

拜拜、爱过 提交于 2019-12-08 02:36:16
问题 XML sent from code to the SQL server. My XML as looks like this: Declare @MainXML XML= '<root> <a>JJ</a> <a>KK</a> </root>' From the above xml, I want to store 2 as being the count of child elements "a" under a column1 in SQL. Column1 ------- 2 Can you help me with this? 回答1: I'd do this with xpath : select @MainXML.query('count(root/a)') sql fiddle demo 回答2: If you're using mssql server then you can take advantage of the OPENXML support. You can get the count of the a nodes by using a SELECT

Add an attribute to the XML Column from another column in the same/another table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 01:26:27
问题 Here's my scenario: --ORDER table OrderID OrderCode DateShipped ShipmentXML 1 ABC 08/06/2013 <Order><Item CustomerName="BF" City="Philadelphia" State="PA"></Item></Order> 2 XYZ 08/05/2013 <Order><Item CustomerName="TJ" City="Richmond" State="VA"></Item></Order> At some point in the process, I will know the respective TrackingNumber for these Orders. The tracking numbers are available in another table like this: --TRACKING table TrackingID OrderCode TrackingNumber 98 ABC 1Z1 99 XYZ 1Z2 The

XQuery exists check in select sql query

瘦欲@ 提交于 2019-12-08 01:06:25
问题 I have one sql table with xml column which holds the value like below xml <Security xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Dacl> <ACEInformation> <UserName>Authenticated Users</UserName> <Access>Allow</Access> <IsInherited>false</IsInherited> <ApplyTo>This object only</ApplyTo> <Permission>List Contents</Permission> <Permission>Read All Properties</Permission> <Permission>Read Permissions</Permission> </ACEInformation>

SQL Server Update Value XML Node

谁都会走 提交于 2019-12-07 20:00:22
问题 I have 2 tables in SQL Server Table1 ID - Name - Phone 1 HK 999 2 RK 888 3 SK 777 4 PK 666 Table2 ID - XMLCol 1 XMLVal1 XMLVal1 <Root> <Data1> <ID>1</ID> <Name>HK</Name> </Data1> <Data1> <ID>2</ID> <Name>RK</Name> </Data1> </Root> Now I am inserting a GUID column into Table1 Table1 ID - Name - Phone - GUID 1 HK 999 HJHHKHJHJHKJH8788 2 RK 888 OONMNy7878HJHJHSD 3 SK 777 POMSDHBSNB775SD87 4 PK 666 HRBMASJMN76448NDN In Table2 XML column, I want to update the ID node with the new GUID value

Count the number of sub tags from XML in SQL

半世苍凉 提交于 2019-12-06 13:26:41
XML sent from code to the SQL server. My XML as looks like this: Declare @MainXML XML= '<root> <a>JJ</a> <a>KK</a> </root>' From the above xml, I want to store 2 as being the count of child elements "a" under a column1 in SQL. Column1 ------- 2 Can you help me with this? I'd do this with xpath : select @MainXML.query('count(root/a)') sql fiddle demo If you're using mssql server then you can take advantage of the OPENXML support. You can get the count of the a nodes by using a SELECT on the parsed XML: Declare @MainXML XML = '<root> <a>JJ</a> <a>KK</a> </root>' SELECT COUNT(MainXML.A.value('.',

SQL XML Xquery data query using a variable in the node selection?

谁说胖子不能爱 提交于 2019-12-06 10:30:53
I am obviously missing something right in front of me, however I have this SQL 2008 XML query as follows: select distinct cast(customFields_xml.query('data(/root/cf_item_type)') as varchar) as c1 from designs .. what I am actually trying to achieve is to make the "cf_item_type" a variable, because I want to pass in the node as a param to a proc.. So in reality, I am trying to end up with something like: (@cf would be passed as a param, but declaring for example use) declare @cf varchar set @cf='cf_item_type' select distinct cast(customFields_xml.query('data(/root/@cf)') as varchar) as cloth

Add an attribute to the XML Column from another column in the same/another table

丶灬走出姿态 提交于 2019-12-06 09:39:14
Here's my scenario: --ORDER table OrderID OrderCode DateShipped ShipmentXML 1 ABC 08/06/2013 <Order><Item CustomerName="BF" City="Philadelphia" State="PA"></Item></Order> 2 XYZ 08/05/2013 <Order><Item CustomerName="TJ" City="Richmond" State="VA"></Item></Order> At some point in the process, I will know the respective TrackingNumber for these Orders. The tracking numbers are available in another table like this: --TRACKING table TrackingID OrderCode TrackingNumber 98 ABC 1Z1 99 XYZ 1Z2 The output I'm expecting is as below: OrderID OrderCode ShipmentXML 1 ABC <Order><Item CustomerName="BF" City=

Convert C# 2.0 System.Data.SqlTypes.SqlXml object into a System.Xml.XmlNode

懵懂的女人 提交于 2019-12-06 05:05:23
I seem to always have problems with converting data to and from XML in C#. It always wants you to create a full XMLDocument object even when you think you shouldn't have to. In this case I have a SQLXML column in a MS SQL 2005 server that I am trying to pull out and push into a function that requires an XMLNode as a parameter. You would think this would be easy, but outside of converting it to a string and creating a new XMLNode object I cannot figure out the right way to do it. I can use an SqlDataReader, the sqlComm.ExecuteReader() to load the reader, and sqlReader.GetSqlXml(0) to get the

Concatenating xml values when selecting by XQuery in T-SQL

拟墨画扇 提交于 2019-12-06 01:35:30
问题 This is my sample XML: <root> <element> <subelement> <value code="code1">value1</value> <value code="code2">value2</value> </subelement> </element> </root> And this is my test query: DECLARE @tempTable TABLE ( ValueCode nvarchar(MAX), Value nvarchar(MAX) ) DECLARE @xml XML select @xml = cast(c1 as xml) from OPENROWSET (BULK 'C:\test.xml', SINGLE_BLOB) as T1(c1) INSERT INTO @tempTable SELECT Tbl.Col.value('subelement[1]/@code', 'NVARCHAR(MAX)'), Tbl.Col.value('subelement[1]', 'NVARCHAR(MAX)')

How to return XML from SQL Server 2008 that is structured with multiple selections sharing a common parent

核能气质少年 提交于 2019-12-05 10:57:51
I've tried using "FOR XML PATH", "FOR XML EXPLICIT" and "FOR XML AUTO" but the data is never structured with the correct heirarchy. Basically, I have one parent table (Customers) and 3 child tables. Each table has a customerid column. There is a one-to-many relationship from the Customers table to each of the 3 child tables. As a mock example, I have a parent "Customers" table, and I have 3 other tables - Products, Hobbies and Vehicles - all related to the Customers table by a customerid. What is the SQL code to achieve the following kind of structure - <Customers> <Customer customerid="1"