1

我有一个 SQL Server 2008 数据库,它将一些 XML 数据存储在表的 varchar 字段中。这用于表示 asp.net 页面中的一次旅行,因此我需要一种方法来获取该数据并将所有成本值相加以获得该值的总成本。

XML 字符串是许多其他字符串之一。可以有多个匹配的字符串。它说以下,

------------更新的 XML 模式添加了编辑--------

<trip>
   <toolboxitemadded id="8ce489fa-acb7-40e9-8440-cad46e16ca65" class="ToolBoxItemAdded" version="1">
    <control id="Location" type="text" value="Mattison, LLC-David Taylor" tag="INPUT" />
    <control id="Description" value="Mattison, LLC&#xA;PO Box 7137&#xA;Statesville, NC 28687, United States&#xA;Tel:704-838-1991 Ext 209&#xA;E-Mail:david@mattison-consulting.com" tag="TEXTAREA" />
       <control id="Visibility_Customer" type="checkbox" value="true" tag="INPUT" />
       <control id="Visibility_Bidder" type="checkbox" value="true" tag="INPUT" />
       <control id="Visibility_Status" type="checkbox" value="true" tag="INPUT" />
       <control id="Reserved_expires" type="text" value="7/22/2012 17:47" tag="INPUT" />
       <control id="Reserved_contacts" type="hidden" value="2366" tag="INPUT" />
   </toolboxitemadded>
   <toolboxitemadded id="8a19f19b-79a8-4d1a-a12c-a4378160116e" class="ToolBoxItemAdded" version="1">
       <control id="TypeOfItem" value="Pallet" tag="SELECT" />
       <control id="NumberOfItems" type="text" value="4" tag="INPUT" />
       <control id="Description" value="Some cool stuff" tag="TEXTAREA" />
       <control id="BreaksDown" type="checkbox" value="false" tag="INPUT" />
       <control id="Length" type="text" value="48" tag="INPUT" />
       <control id="Width" type="text" value="48" tag="INPUT" />
       <control id="Height" type="text" value="48" tag="INPUT" />
       <control id="Weight" type="text" value="2000" tag="INPUT" />
       <control id="MeasurementType" value="Standard" tag="SELECT" />
       <control id="Hazmat" type="checkbox" value="false" tag="INPUT" />
       <control id="UNCode" type="text" value="" tag="INPUT" />
       <control id="ClassCode" type="text" value="" tag="INPUT" />
       <control id="Visibility_Customer" type="checkbox" value="true" tag="INPUT" />
       <control id="Visibility_Bidder" type="checkbox" value="true" tag="INPUT" />
       <control id="Visibility_Status" type="checkbox" value="false" tag="INPUT" />
  </toolboxitemadded>
  <toolboxitemadded id="ae33c118-2981-4a3e-b001-c6dc56d0e7f1" class="ToolBoxItemAdded" version="1">
    <control id="AirportName" type="text" value="KCLT" tag="INPUT" />
    <control id="ICAO" type="text" value="KCLT" tag="INPUT" />
    <control id="IATA" type="text" value="CLT" tag="INPUT" />
    <control id="Address" value="Charlotte" tag="TEXTAREA" />
    <control id="Description" value="" tag="TEXTAREA" />
    <control id="Visibility_Customer" type="checkbox" value="true" tag="INPUT" />
    <control id="Visibility_Bidder" type="checkbox" value="true" tag="INPUT" />
    <control id="Visibility_Status" type="checkbox" value="true" tag="INPUT" />
    <control id="Reserved_expires" type="text" value="7/22/2012 17:48" tag="INPUT" />
    <control id="Reserved_cost" type="text" value="15000" tag="INPUT" />
    <control id="Reserved_markup" type="text" value="" tag="INPUT" />
  </toolboxitemadded>
  <toolboxitemadded id="4aea1ef1-e0fd-482c-9d64-217b7f891e61" class="ToolBoxItemAdded" version="1">
    <control id="Description" type="text" value="Falcon test 3" tag="INPUT" />
    <control id="TailNumber" type="text" value="MP123D" tag="INPUT" />
    <control id="FlightNumber" type="text" value="12334" tag="INPUT" />
    <control id="Carrier" type="text" value="[object Object]" tag="INPUT" />
    <control id="Model" type="text" value="Desault Falcon 20" tag="INPUT" />
    <control id="AircraftNotes" value="Some aircraft" tag="TEXTAREA" />
    <control id="Visibility_Customer" type="checkbox" value="false" tag="INPUT" />
    <control id="Visibility_Bidder" type="checkbox" value="false" tag="INPUT" />
    <control id="Visibility_Status" type="checkbox" value="true" tag="INPUT" />
    <control id="Reserved_cost" type="text" value="20000" tag="INPUT" />
    <control id="Reserved_markup" type="text" value="" tag="INPUT" />
  </toolboxitemadded>
  <toolboxitemadded id="ae33c118-2981-4a3e-b001-c6dc56d0e7f1" class="ToolBoxItemAdded" version="1">
    <control id="AirportName" type="text" value="ATL" tag="INPUT" />
    <control id="ICAO" type="text" value="KATL" tag="INPUT" />
    <control id="IATA" type="text" value="ATL" tag="INPUT" />
    <control id="Address" value="Atlanta" tag="TEXTAREA" />
    <control id="Description" value="" tag="TEXTAREA" />
    <control id="Visibility_Customer" type="checkbox" value="true" tag="INPUT" />
    <control id="Visibility_Bidder" type="checkbox" value="true" tag="INPUT" />
    <control id="Visibility_Status" type="checkbox" value="true" tag="INPUT" />
    <control id="Reserved_expires" type="text" value="7/22/2012 17:48" tag="INPUT" />
    <control id="Reserved_cost" type="text" value="22000" tag="INPUT" />
    <control id="Reserved_markup" type="text" value="" tag="INPUT" />
  </toolboxitemadded>

------------更新的 XML 模式添加了编辑--------

这不是所有的代码,但应该有很多可以理解它是如何组合在一起的,

我需要从字段中解析这个 xml 字符串的每个实例,然后获取这些值并将它们一起添加,以便报告可以给出旅行的总价格,但我不知道如何做到这一点。

有任何想法吗?

4

1 回答 1

0

看起来像 XML,所以运气好的话,您可以将varchar列转换为 XML 并在 XQuery 表达式中求和,同时获取值。

declare @T table
(
  Cost nvarchar(max)
)

insert into @T values
('<control id="Reserved_cost" type="text" value="20000" tag="INPUT" /><control id="Reserved_cost" type="text" value="20000" tag="INPUT" />'),
('<control id="Reserved_cost" type="text" value="20000" tag="INPUT" />')

select cast(T.Cost as xml).value('sum(//control/@value)', 'int') as CostSum
from @T as T

结果:

CostSum
-----------
40000
20000

更新:

您可以在 XQuery 中的方括号之间添加谓词。
使用它仅对属性id具有值的节点求和Reserved_cost

select cast(T.Cost as xml).value('sum(//control[@id="Reserved_cost"]/@value)', 'int') as CostSum
from @T as T
于 2012-09-07T19:20:06.727 回答