How to update specific XML attributes in SQL Server? - sql

We are storing an XML document of all the different costs in SQL Server 2008 r2. Now, certain prices need to be update based on a formula.
One approach to this would be to:
Shred the XML and access the values.
Do the calculation
Now rebuild the XML structure.
Is this the only way to do it? Or is there an easier way where we can access the values on the fly and update the entire XML in one go? For this kind of scenario, what is the best approach?
Here is the sample of the XML:
<ItemAttributeData>
<Section ID="469919" TypeID="61000002">
<Attributes>
<Attribute ID="3870" UniqueID="1c71212b-8395-4b43-a44d-9a63374c8c99" VALUE="2" />
<Attribute ID="3569" UniqueID="c0b754d3-e03e-4d4a-9bc1-dcb11c04535b" VALUE="2" />
<Attribute ID="1592" UniqueID="2609b344-88b2-44d6-be20-07b634705c30" VALUE="2" />
</Attributes>
</Section>
<Section ID="469920" TypeID="61000001">
<Attributes>
<Attribute ID="3702" UniqueID="7cabf9e8-4f60-4e1e-afe5-75b153e8c5eb" VALUE="1000001649" />
<Attribute ID="3870" UniqueID="595f8e2c-bccb-4fcb-81d4-303fcb7d44c4" VALUE="2" />
<Attribute ID="3868" UniqueID="2226f6c6-380a-4195-98f7-c6deec84e4e8" VALUE="2" />
<Attribute ID="3754" UniqueID="bf936c3f-fa96-49b9-8a62-46fb7e0009d5" VALUE="1000001600" />
<Attribute ID="2204" UniqueID="bdc78784-86ad-4567-bc06-40896d603eaf" VALUE="1" />
<Attribute ID="3874" UniqueID="8728249f-5e60-4938-a60d-208426fe11c8" VALUE="1" />
</Attributes>
</Section>
<Section ID="469921" TypeID="61000001" />
</ItemAttributeData>

You can use non standard XML update command replace value of. Like
DECLARE #myDoc xml
SET #myDoc = '<Root>
<Location LocationID="10"
LaborHours=".1"
MachineHours=".2" >Manu steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
--SELECT #myDoc
SET #myDoc.modify('
replace value of (/Root/Location[1]/#LaborHours)[1]
with (
if (count(/Root/Location[1]/step) > 3) then
"3.0"
else
"1.0"
)
')
SELECT #myDoc

Related

A third column with values “dependant” on two or more other columns in an iGIS .glc-addon to shapefile?

I'm working with a protocol for aiding field data collection, the platform I'm using is the iOS version of GeometryITs iGIS app.
I've created a .glc-file with lookup tables associated to a .shp-file to structure the way my colleagues are collecting their field data, it looks like this:
<?xml version="1.0" ?>
<GISLayerConfiguration>
<!-- A textual description of this configuration -->
<!--
<description></description>
-->
<!--
A CodeList is used to specify Name-Value-Pairs to be used in database tables.
These Name-Value-Pairs are stored in 1 table, the lookup table, and then the
codes are stored in other tables. Applications can then replace the code with
the value to present to humans.
The CodeList element must have name, type and length attributes.
The name is used to create the lookup table.
The type and length represent the code, not the value. This allows
tables that are to be used with the CodeList to be validated.
A value element represents 1 Name-Value-Pair.
The code attribute is used to reference the value in other tables.
-->
<Attribute column="Tradart" readable-name="Trädart">
<Value data-type="N" length="10">
<field-type>CodeList</field-type>
<field-param>Tradart</field-param>
<default>1</default>
<prompt></prompt>
</Value>
<Visibility>
<list>true</list>
<edit>true</edit>
</Visibility>
</Attribute>
<CodeList name="" type="" length="">
<value code=""></value>
</CodeList>
<CodeList name="Tradart" type="N" length="3">
<value code="1">Alm</value>
<value code="2">Ask</value>
<value code="3">Bok</value>
<value code="4">Ek</value>
<value code="5">Gran</value>
<value code="6">Hägg</value>
<value code="7">Lönn, parklind</value>
<value code="8">Oxel</value>
<value code="9">Rönn</value>
<value code="10">Sälg</value>
<value code="11">Tall</value>
<value code="12">Triviallöv</value>
</CodeList>
<Attribute column="Diam_cm_in" readable-name="Diameter i centimeter - intervall">
<Value data-type="N" length="10">
<field-type>CodeList</field-type>
<field-param>Diam_cm_in</field-param>
<default>1</default>
<prompt></prompt>
</Value>
<Visibility>
<list>true</list>
<edit>true</edit>
</Visibility>
</Attribute>
<CodeList name="" type="" length="">
<value code=""></value>
</CodeList>
<CodeList name="Diam_cm_in" type="N" length="3">
<value code="1">0-30 cm</value>
<value code="2">31-40 cm</value>
<value code="3">41-50 cm</value>
<value code="4">51-60 cm</value>
<value code="5">61-70 cm</value>
<value code="6">71-80 cm</value>
<value code="7">81-90 cm</value>
<value code="8">91-100 cm</value>
<value code="9">Över 101 cm</value>
</CodeList>
</GISLayerConfiguration>
Depending on the values entered in and I want to assign a value to a third column.
The command could look something like this:
IF <Attribute column="Tradart" value="1"> AND <Attribute column="Diam_cm_in"="7","8","9"> THEN <Attribute column="Results" value = "1">
Any ideas on how to make this work? I'm a bit embarrassed to admit I don't even know what language in using, and therefore not which syntax to use...
This post is actually a re-post from earlier today, when I posted on http://gis.stackexchange.com
I realized this is probably a topic more related to actual XML or SQL programming than the general GIS topic.

Execution of Fetch XML using Web API Dynamics 365

I am using this approach for paging using Fetch XML within Web API.
Web API works perfect when we use simple fetch and it returns pagging cookie along with results if there are more records but when we use complex(where we have link entities) Fetch XML it returns paging cookie but empty e.g.:
<b><cookie pagenumber="2" pagingcookie="" istracking="False" /></b>
Here you can see we have nothing in pagingcookie.
Fetch XML used for query:
<fetch mapping="logical" version="1.0" output-format="xml-platform" count="10" page="1" >
<entity name="invoicedetail" >
<attribute name="invoicedetailid" />
<attribute name="uomid" />
<attribute name="quantity" />
<attribute name="manualdiscountamount" />
<attribute name="priceperunit" />
<attribute name="extendedamount" />
<filter>
<condition entityname="invoice" attribute="customerid" operator="eq" value="{5A8F8B46-2443-E511-80E3-3863BB351E10}" />
</filter>
<link-entity name="invoice" from="invoiceid" to="invoiceid" >
<attribute name="invoiceid" />
<attribute name="invoicenumber" />
<attribute name="description" />
<attribute name="totalamount" />
<order attribute="ss_postingdate" descending="true" />
</link-entity>
</entity>
</fetch>
If we remove linked entity it starts giving the exact paging cookie in the response that can be get using #Microsoft.Dynamics.CRM.fetchxmlpagingcookie.
Is there any way to get the exact paging cookie in complex scenarios?
I've seen this happen before in the standard organization service. I would suggest just paging without the paging cookie - this resolved the issue for me in the organization service.
E.g. <fetch mapping="logical" page="1" count="50">

Dynamics CRM 2015 - Retrieve all Contact that are in any way connected to Opportunity

I need to present all contact that have connection to opportunity in dialog. So when some opportunity has Sales Team, Stakeholders, Owners, etc. Something like Active Connection Associate View. I need a FetchXml or other ways to find all the Contact that are in any way connected to current opened Opportunity.
Your fetchXML query will look quite like this :
<fetch mapping='logical' version='1.0'>
<entity name='myentity'>
<filter>
<condition attribute='myentityid' operator='eq' value='myvalue' />
</filter>
<link-entity name='secondentity' from='stsr_materialid' to='stsr_material_casinglookup' alias='c' link-type='outer'>
<attribute name='stsr_name' />
<attribute name='stsr_code_aisi' />
<attribute name='stsr_code_astm' />
<attribute name='stsr_code_din' />
<attribute name='stsr_code_e' />
<attribute name='stsr_code_en' />
</link-entity>
If you need more help, please provide more context.

how to get a cross Joins in Fetch XML queries in CRM2011?

1.-First Time I do Queries as it then I dont know how to get a cross join
2.-I understand this example is a inner join isn't it? I take it for this line "from="accountid" to="customerid"" this sees as a Inner Join
then how would it be a cross join?
3.- I copied this example on http://mscrmshop.blogspot.mx/2012/09/outer-joins-in-fetch-xml-queries-in.html
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="opportunity">
<attribute name="name" />
<attribute name="customerid" />
<attribute name="estimatedvalue_base" />
<order attribute="name" descending="false" />
<link-entity name="account" from="accountid" to="customerid" visible="false" link-type="outer" alias="accountid">
<attribute name="telephone1" />
</link-entity>
</entity>
</fetch>
Fetch XML does not support a cross Join.
There is another alternative solution suggested in the below link. Please see whether that can be useful for your case.
http://social.microsoft.com/Forums/en-US/e3ee734c-81d3-4277-b54f-c2e46bb20e0d/crm-2011-sql-cross-join-equivalent-fetchxml-in-report?forum=crmdevelopment

SSRS can't use "Required Attendees" in dataset?

I am creating report in SSRS for CRM. I have a table with Dataset for Appointment info and I noticed that it does not allow me to report on required attendees??? Is this true? If so is there a workaround around this? I basically want my report to show appointments details and of course who participated.
Any advice is much appreciated!
Below is the fetchXML I gathered from using Advanced Find in CRM 2013.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="appointment">
<attribute name="subject"/>
<attribute name="statecode"/>
<attribute name="scheduledstart"/>
<attribute name="activityid"/>
<attribute name="instancetypecode"/>
**<attribute name="requiredattendees"/>**
<order attribute="subject" descending="false"/>
<link-entity name="account" from="accountid" to="regardingobjectid" alias="ae">
<filter type="and">
<condition attribute="accountid" operator="eq" value="#name"/>
</filter>
</link-entity>
</entity>
</fetch>
To get this information you will have to create another one datasource and retrieve information from ActivityParty entity. All items like requiredattendees, optionalattendees in appointment, from, to, cc, bcc in email, e.t.c. are stored inside ActivityParty entity.