T-SQL how to have multiple criteria in xml.exist function - sql-server

This code works, but I need to have multiple criteria in the .exist method. "where State = 'FL' And Department = 'HR'" within the same Objects node. I've tried a lot of ways, but no luck. Anyone know how?
Thank you in adance.
If OBJECT_ID(N'tempdb..#xml') Is Not Null
Drop Table #xml
Create Table #Xml
(
RecordId INT IDENTITY(10,1) NOT NULL PRIMARY KEY,
XmlData XML NOT NULL
)
GO
Declare #xml xml =
'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="John" />
<Object Name="LastName" Value="Smith" />
<Object Name="City" Value="Miami" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="HR" />
</Objects>
</DataObject>'
Insert #xml select #xml
Set #xml =
'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="Jane" />
<Object Name="LastName" Value="Doe" />
<Object Name="City" Value="Hollywood" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="Accounting" />
</Objects>
</DataObject>'
Insert #xml select #xml
Declare #Dept varchar(30) = 'HR', #State varchar(30) = 'FL'
Select RecordID
From #Xml
Where XMLData.exist('//Objects[Object[#Name="State"][#Value=sql:variable("#State")]]') = 1

Please try the following. The XPath expression is checking exactly for what you need.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE
(
RecordId INT IDENTITY(10,1) PRIMARY KEY,
XmlData XML NOT NULL
);
INSERT INTO #tbl (XmlData)
VALUES (N'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="John" />
<Object Name="LastName" Value="Smith" />
<Object Name="City" Value="Miami" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="HR" />
</Objects>
</DataObject>')
, (N'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="Jane" />
<Object Name="LastName" Value="Doe" />
<Object Name="City" Value="Hollywood" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="Accounting" />
</Objects>
</DataObject>');
-- DDL and sample data population, end
DECLARE #Dept VARCHAR(30) = 'HR', #State CHAR(2) = 'FL';
SELECT RecordID
FROM #tbl
WHERE XMLData.exist('/DataObject/Objects[Object[#Name="State" and #Value=sql:variable("#State")]
and Object[#Name="Department" and #Value=sql:variable("#Dept")]]') = 1;
Output
+----------+
| RecordID |
+----------+
| 10 |
+----------+

Related

How to parse XML which is stored in a column in SQL Server

I want to parse XML which is stored in a column in SQL Server:
Table column with XML in this picture
and the XML is:
<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0" type="collection">
<collection>
<propertylist id="1396440519721" />
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>
How can I parse the values of workitemid, specid?
I have written the following code
SELECT
ID,
ParentPropertyId = xc.value('(../#id)[1]', 'varchar(100)'),
WorkItemId = xc.value('(.)[1]', 'varchar(100)')
FROM
scheduleplanitem
CROSS APPLY
valuetree.nodes('//propertylist/property[#id="workitemid"]') xt(xc)
but I get an error
The XMLDT method 'nodes' can only be invoked on columns of type xml
The result should be:
workitemid SOURAVTESTER
specid EMSpec
Please try the following solution.
It does the following:
Removes XML prolog with the UTF-8 encoding. SQL Server implicitly converts any XML internally to UTF-16.
Converts NVARCHAR() data type to XML data type. After that XQuery
methods to handle XML become available.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, col NVARCHAR(MAX));
INSERT INTO #tbl (col) VALUES
(N'<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0"
type="collection">
<collection>
<propertylist id="1396440519721"/>
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>')
, (NULL);
-- DDL and sample data population, end
--DECLARE #startPos INT = LEN('<?xml version="1.0" encoding="utf-8"?>') + 1;
DECLARE #prolog NVARCHAR(100) = '<?xml version="1.0" encoding="utf-8"?>';
;WITH rs AS
(
SELECT ID
--, TRY_CAST(SUBSTRING(col
-- , #startPos
-- , LEN(col)) AS XML) AS xmldata
, TRY_CAST('<root>' + REPLACE(col, #prolog, '') + '</root>' AS XML) AS xmldata FROM #tbl
)
SELECT ID
, c.value('(./text())[1]', 'VARCHAR(100)') AS Item
FROM rs
CROSS APPLY xmldata.nodes('//property[#id=("specid","workitemid")]') AS t(c);
Output
+----+----------------+
| ID | Item |
+----+----------------+
| 1 | SOURAVTESTER|1 |
| 1 | EMSpec|1 |
+----+----------------+

How to read an option from XML in T-SQL?

I have the next XML file:
<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>
I need to read some value from any option of XML-node, for e.g. I need to read the value of Type option from Field node using T-SQL.
How can I do this using internal OpenXML provider in MSSQL?
The following will extract all the data from your XML:
DECLARE #doc VARCHAR(1000) = '<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>';
DECLARE #iDoc INT;
EXECUTE sp_xml_preparedocument #idoc OUTPUT, #doc;
SELECT *
FROM OPENXML(#iDoc, 'DynamicEntity/Field')
WITH
( DynamicEntityName VARCHAR(100) '../#Name',
FieldName VARCHAR(100) '#Name',
[Type] VARCHAR(100) '#Type',
AllowNulls VARCHAR(100) '#AllowNulls'
);
Basically, you just need to specify a column mapping for your xml attributes.
Here is a solution:
DECLARE #x XML = '<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>'
SELECT t.c.value(N'../#Name', N'nvarchar(100)'),
t.c.value(N'#Name', N'nvarchar(100)'),
t.c.value(N'#Type', N'nvarchar(100)'),
t.c.value(N'#AllowNulls', N'nvarchar(100)')
FROM #x.nodes(N'/DynamicEntity/Field') t(c)

Bind xml file to datagrid in silverlight

I am working on silverlight to bind a xml file to datagrid.I have found many example but my xml file is very complex. so how to read or bind it to data grid.
Below is my xml file and i want to read element "EntityType" with its sub element and its attribute values.
Thanks.
`<?xml version="1.0" encoding="utf-8" ?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="Microsoft.Crm.Sdk.Data.Services" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<EntityType Name="SdkMessageRequestField">
<Key>
<PropertyRef Name="SdkMessageRequestFieldId" />
</Key>
<Property Name="FieldMask" Type="Edm.Int32" Nullable="true" />
<Property Name="Name" Type="Edm.String" Nullable="true" />
</EntityType>
<ComplexType Name="EntityReference">
<Property Name="Id" Type="Edm.Guid" Nullable="true" />
<Property Name="LogicalName" Type="Edm.String" Nullable="true" />
<Property Name="Name" Type="Edm.String" Nullable="true" />
</ComplexType>
</Schema>
</edmx:DataServices>
</edmx:Edmx>`
XDocument x = XDocument.Load("XMLFileName.xml");
var a = (from c in x.Descendants("edmx").Elements("Schema").Elements("EntityType/ComplexType")
select new
{
Name = c.Parent.Attribute("Name"),
PropertyName = c.Attribute("Name"),
PropertyType = c.Attribute("Type")
}).ToArray();
foreach (var itm in a)
{
// TODO:.....
}

NHibernate and sql timestamp columns as version

I've been racking my head trying to get Nhibernate to work with a byte
array as version mapping to an sql timestamp. I'd implemented an
IUserVersionType but Nhibernate was creating varbinary in the database
rather than timestamp. Inspired by a blog post by Ayende recently on
concurrency, I changed my mapping to specify the sql-type to timestamp
which worked perfectly. However I now face a rather curious problem
wherein Nhibernate does an insert, gets the new version and then
immediately tries to do an update and attempts to set the version
column, which being an sql timestamp fails.
This is my mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Contact" table="Contacts" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="Title" type="String">
<column name="Title" length="5" />
</property>
<property name="FirstName" type="String">
<column name="FirstName" not-null="true" length="50" />
</property>
<property name="MiddleName" type="String">
<column name="MiddleName" length="50" />
</property>
<property name="LastName" type="String">
<column name="LastName" not-null="true" length="50" />
</property>
<property name="Suffix" type="String">
<column name="Suffix" length="5" />
</property>
<property name="Email" type="String">
<column name="Email" length="50" />
</property>
<bag name="PhoneNumbers" inverse="true" cascade="all-delete-
orphan">
<key foreign-key="FK_Contacts_PhoneNumbers_ContactId" on-
delete="cascade" column="ContactId" />
<one-to-many class="Core.Domain.Entities.PhoneNumber,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" />
</bag>
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Customer" table="Customers" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="AccountNumber" access="nosetter.pascalcase-
underscore" type="String">
<column name="AccountNumber" unique="true" length="25" />
</property>
<!-- other mappings... -->
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
<joined-subclass name="Core.Domain.Entities.Individual,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Individuals">
<key column="CustomerId" />
<many-to-one fetch="join" lazy="false" not-null="true"
cascade="all" unique="true" not-found="exception" name="Contact"
column="ContactID" />
<bag name="Addresses" table="Addresses_Individuals">
<key column="AddressId" foreign-
key="FK_Addresses_Individuals_Addresses_AddressId" />
<many-to-many column="IndividualId"
class="Core.Domain.Entities.Address, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Addresses_Individuals_Individuals_IndividualId" />
</bag>
</joined-subclass>
<joined-subclass name="Core.Domain.Entities.Store, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Stores">
<key column="CustomerId" />
<many-to-one unique="true" cascade="save-update" fetch="join"
not-null="true" not-found="exception" name="Address"
column="AddressId" />
<many-to-one lazy="proxy" not-null="true" cascade="all" not-
found="exception" name="Client" column="ClientId" />
<property name="StoreName" type="String">
<column name="StoreName" not-null="true" length="50" />
</property>
<bag name="Contacts" table="Contacts_Stores">
<key column="ContactId" foreign-
key="FK_Contacts_Stores_Contacts_ContactId" />
<many-to-many column="StoreId"
class="Core.Domain.Entities.Contact, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Contacts_Stores_Stores_StoreId" />
</bag>
</joined-subclass>
</class>
</hibernate-mapping>
Calling Session.Save on an Individual with associated Contact results
in the following error:
NHibernate: INSERT INTO Addresses (Line1, PostalCode, Country,
DateCreated, DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3,
#p4, #p5); select SCOPE_IDENTITY(); #p0 = 'Order Address Line 1', #p1
= 'CV31 6BW', #p2 = 'United Kingdom', #p3 = '20/04/2009 19:45:32', #p4
= '20/04/2009 19:45:32', #p5 = ''
NHibernate: SELECT address_.Version as Version22_ FROM Addresses
address_ WHERE address_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Contacts (FirstName, LastName, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3, #p4); select
SCOPE_IDENTITY(); #p0 = 'Joe', #p1 = 'Bloggs', #p2 = '20/04/2009
19:45:34', #p3 = '20/04/2009 19:45:34', #p4 = ''
NHibernate: SELECT contact_.Version as Version33_ FROM Contacts
contact_ WHERE contact_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Customers (AccountNumber, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3); select
SCOPE_IDENTITY(); #p0 = '', #p1 = '20/04/2009 19:45:34', #p2 =
'20/04/2009 19:45:34', #p3 = ''
NHibernate: INSERT INTO Individuals (ContactID, CustomerId) VALUES
(#p0, #p1); #p0 = '1', #p1 = '1'
NHibernate: SELECT individual_1_.Version as Version2_ FROM Individuals
individual_ inner join Customers individual_1_ on
individual_.CustomerId=individual_1_.Id WHERE
individual_.CustomerId=#p0; #p0 = '1'
NHibernate: UPDATE Contacts SET Version = #p0 WHERE Id = #p1 AND
Version = #p2; #p0 = 'System.Byte[]', #p1 = '1', #p2 = 'System.Byte[]'
System.Data.SqlClient.SqlException: Cannot update a timestamp column.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning
(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader
ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds
(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean
returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery
(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
in c:\CSharp\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:
line 203
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object
id, Object[] fields, Object[] oldFields, Object rowId, Boolean[]
includeProperty, Int32 j, Object oldVersion, Object obj,
SqlCommandInfo sql, ISessionImplementor session) in c:\CSharp\NH
\nhibernate\src\NHibernate\Persister\Entity
\AbstractEntityPersister.cs: line 2713
NHibernate.Exceptions.GenericADOException: could not update:
[Core.Domain.Entities.Contact#1][SQL: UPDATE Contacts SET Version =
#p0 WHERE Id = #p1 AND Version = #p2]
Any ideas why NHibernate is attempting to update the version column
for Contact, even though it didn't for the Address?
I have found that using dynamic-insert="true" on the class along with causes this issue. I use the following mapping successfully:
...
<class name="Contact" table="Contact">
<id name="ID" column="ID" type="int">
<generator class="identity" />
</id>
<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob"/>
...
The Address doesn't have a version column I assume.
I wonder where you have the sql-type from. Why not this way?
<version name="Version" type="Timestamp" generated="always" unsaved-value="null">
<column name="Version" not-null="false" />
</version>
You need of course a DateTime in the entity.
http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx

SQL Server xml.modify delete method

I have a problem with removing an attribute from a node.
Example:
DECLARE #processID int
SET #processID = 8
DECLARE #xml XML
SET #xml =
'<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" subProcessID="8">
</Shape>
<Shape id="2" name="Shape 2" subProcessID="9">
</Shape>
</Shapes>
<Lines />
</Process>'
SET #xml.modify('delete (/Process/Shapes/Shape/#subProcessID[/Process/Shapes/Shape/#subProcessID = sql:variable("#processID")])')
SELECT #xml
Gives the result:
<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" />
<Shape id="2" name="Shape 2" />
</Shapes>
<Lines />
</Process>
What I would like is:
<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" />
<Shape id="2" name="Shape 2" subProcessID="9" />
</Shapes>
<Lines />
</Process>
What is the syntax to achieve this?
As the OP is gone but he left the solution in a comment let me add that as an answer:
DECLARE #processID int
SET #processID = 8
DECLARE #xml XML
SET #xml =
'<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" subProcessID="8">
</Shape>
<Shape id="2" name="Shape 2" subProcessID="9">
</Shape>
</Shapes>
<Lines />
</Process>'
SET #xml.modify('delete (/Process/Shapes/Shape[#subProcessID = sql:variable("#processID")]/#subProcessID)')
SELECT #xml
Here is a working sqlfiddle for that.

Resources