Join XML columns from SQL Server 2016+ - sql-server

I have a TABLE_A with an XML column mycategoryXML which contains this XML:
<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>
I also have another TABLE_B with a XML column called mymappedids
<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>
Is it possible to select all Category nodes from TABLE_A and also the correct names for each category from table_B, so I ultimately get the follow result:
id | categoryid | categoryname
---+------------+-------------
1 | 1 | categoryname1
2 | 2 | categoryname2
3 | 3 | categoryname3
with one query from SQL Server?

There are several approaches but you did not tell us enough about your background.
Especially unclear: How is the connection between these two tables a and b?
You might try something along this:
A mockup scenario to simulate your issue (please provide this yourself in your next question):
DECLARE #tblA TABLE(aId INT,Xml1 XML);
DECLARE #tblB TABLE(bId INT,Xml2 XML);
INSERT INTO #tblA(aId,Xml1) VALUES
(100
,N'<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>');
INSERT INTO #tblB(bId,Xml2) VALUES
(200
,N'<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>');
--The query
SELECT a.aId
,aXmlValues.*
,(SELECT Xml2.value('(/mapids
/ids
/myid[#id=sql:column("aXmlValues.CategoryId")]
/#name)[1]','nvarchar(max)')
FROM #tblB b
WHERE bID=200 /*however you find this...*/) AS CategoryName
FROM #tblA a
CROSS APPLY a.Xml1.nodes('/mainxml/category') aXml(c)
CROSS APPLY(SELECT aXml.c.value('#id','int') AS CategoryId
,aXml.c.value('#ftype','int') AS CategoryType) aXmlValues;
The idea in short:
We use APPLY ... .nodes() to get the categories of table "a" in separate rows (a derived set).
now we can use APPLY again. This is a trick to get the XML's value into the result set as a normal column.
This column can be placed into XQuery against table "b" using sql:column().

I assume you likely want something like this:
DECLARE #XML1 xml = '<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>';
DECLARE #XML2 xml = '<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>';
SELECT X1.c.value('#id','int') AS id,
X2.mi.value('#id','int') AS categoryid, --Seem silly to have this value twice
X2.mi.value('#name','varchar(30)') AS categoryname
FROM (VALUES(#XML1)) V1(X)
CROSS APPLY (VALUES(#XML2)) V2(X)
CROSS APPLY V1.X.nodes('/mainxml/category')X1(c)
CROSS APPLY V2.X.nodes('/mapids/ids/myid')X2(mi)
WHERE X1.c.value('#id','int') = X2.mi.value('#id','int');

Related

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

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 |
+----------+

SQL Server bulk insert XML format file

I am trying to load a fixed width text file using Bulk Insert and a XML format file. I have used the same process and XML file on another fixed width, except with less columns.
Error
Msg 4857, Level 16, State 1, Line 16
Line 4 in format file "\\PATHC\addr.xml": Attribute "type" could not be specified for this type.
SQL Server Table
create table [dbo].[raw_addr](
address_number varchar(max),
addr_linel varchar(max),
addr_line2 varchar(max),
street_no varchar(max),
street_name varchar(max),
street_type varchar(max),
locality varchar(max),
[state] varchar(max),
country varchar(max),
postcode varchar(max)
);
SQL Server Bulk Insert
BULK INSERT [dbo].[raw_addr] FROM '\\PATH\addr.txt'
WITH (
FORMATFILE = '\\PATH\addr.xml',
ROWTERMINATOR='\r\n');
XML Code:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema_instance">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="9" />
<FIELD ID="2" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="3" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="4" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="5" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="6" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="7" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="8" xsi:type="CharFixed" LENGTH="3" />
<FIELD ID="9" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="10" xsi:type="CharTerm" TERMINATOR="\r\n" LENGTH="4" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="address_number" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="2" NAME="addr_linel" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="3" NAME="addr_line2" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="4" NAME="street_no" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="5" NAME="street_name" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="6" NAME="street_type" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="7" NAME="locality" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="8" NAME="state" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="9" NAME="country" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="10" NAME="postcode" xsi:type="SQLNVARCHAR" />
</ROW>
</BCPFORMAT>
Example TXT extract
001044057C/- XXXXXX XXXXXXX 0000
001295978XXXX 0000
0013974311 0000
00124485712 0000
0012390352 0000
0014720345 0000
0014792876 0000
000986525 ARABANOO GRENFELL NSW 2810
000986589 PO BOX XX KEMPSEY NSW 2440
000740594 'RUSSLEY' ABERDEEN NSW 2336
000311516 BUNYARA ABERDEEN NSW 2336
000298796 CAMBRIA ABERDEEN NSW 2336
000540611 HALCOMBE ABERDEEN NSW 2336
000513112 'LARK HILL' ABERDEEN NSW 2336
000612955 FAN HILL ABERMAIN NSW 2326
001109439 BRAYWOOD ADAMINABY NSW 2630
000460864 TARA ADAMINABY NSW 2630
000315297 ADAMSTOWN NSW 2289
000470057 COORUMBENE ADELONG NSW 2729
000491941 NACKI ADELONG NSW 2729
I think your problem is in the SQL statement, try removing the ROWTERMINATOR. It is not necessary since you specify it in the format file.

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

nHibernate Master Detail Deletion

I have a Master Detail relationship configured. The hbm file is below. When I run some code like this
Favourite favourite = favourites.Find(f => f.Id== id);
user.Favourites.Remove(favourite);
m_UserRepository.Save(ref user);
I get the error message
NHibernate.Exceptions.GenericADOException: could not delete collection rows: [Model.Entities.User.Favourites#249][SQL: SQL not available] ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'UserId', table 'BE.Favourite'; column does not allow nulls. UPDATE fails.
Any suggestions on what this means please help.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model.Entities" schema="BE" assembly="Model" default-lazy="false">
<class name="Model.Entities.User, Model" table="Users" >
<id name="UserId" column="UserId" type="int" unsaved-value="0">
<generator class="native" />
</id>
<property name="UserName" column="UserName" type="string" />
<bag name="Favourites" cascade="all" lazy="true">
<key column="UserId"/>
<one-to-many class="Model.Entities.Favourite, Model"/>
</bag>
</class>
</hibernate-mapping>
Have you tried setting inverse="true" on your bag?
You actually need a many-to-many relationship in this case:
<class name="User">
<id name="Id">
<generator class="native">
<param name="sequence">object_sequence</param>
</generator>
</id>
<version name="Version" />
<property name="Name" />
<set name="Roles" table="User_Favourite">
<key column="UserId"/>
<many-to-many column="FavouriteId" class="Favourite"/>
</set>
</class>
And the same on the other side: (*note the inverse="true")
<class name="Favourite">
<id name="Id">
<generator class="native">
<param name="sequence">object_sequence</param>
</generator>
</id>
<version name="Version" />
<property name="RoleName" />
<set name="Users" table="User_Favourite" inverse="true">
<key column="FavouriteId"/>
<many-to-many column="UserId" class="User"/>
</set>
</class>
From the NHibernate 2.0 Documentation:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.
Finally, I'm unsure if you really want to use a bag here. One user can have the same favorite two or more times?
P.S.: Also, note that lazy="true" is the default behavior since NHibernate 1.2.
try changing your cascade rule to:
<bag name="Favourites" cascade="all,delete-orphan" lazy="true">
<key column="UserId" not-null="true"/>
<one-to-many class="Model.Entities.Favourite, Model"/>
</bag>

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