XQuery的值()方法不能正常工作,并给错误(xquery value() method not

2019-10-24 00:25发布

我有一个示例数据作为XML列的说明 -

<root xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">
                Adventure Works CyclesFR-210B Instructions for Manufacturing HL Touring Frame. Summary: This document contains manufacturing instructions for manufacturing the HL Touring Frame, Product Model 7. Instructions are work center specific and are identified by Work Center ID. These instructions must be followed in the order presented. Deviation from the instructions is not permitted unless an authorized Change Order detailing the deviation is provided by the Engineering Manager.<Location LaborHours="2.5" LotSize="100" MachineHours="3" SetupHours="0.5" LocationID="10">
                    Work Center 10 - Frame Forming. The following instructions pertain to Work Center 10. (Setup hours = .5, Labor Hours = 2.5, Machine Hours = 3, Lot Sizing = 100)<step>
                        Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>.
                    </step><step>
                        Attach <tool>Trim Jig TJ-26</tool> to the upper and lower right corners of the aluminum sheet.
                    </step><step>
                        Using a <tool>router with a carbide tip 15</tool>, route the aluminum sheet following the jig carefully.
                    </step><step>
                        Insert the frame into <tool>Forming Tool FT-15</tool> and press Start.
                    </step><step>
                        When finished, inspect the forms for defects per Inspection Specification <specs>INFS-111</specs>.
                    </step><step>Remove the frames from the tool and place them in the Completed or Rejected bin as appropriate.</step></Location><Location LaborHours="1.75" LotSize="1" MachineHours="2" SetupHours="0.15" LocationID="20">
                    Work Center 20 - Frame Welding. The following instructions pertain to Work Center 20. (Setup hours = .15, Labor Hours = 1.75, Machine Hours = 2, Lot Sizing = 1)<step>
                        Assemble all frame components following blueprint <blueprint>1299</blueprint>.
                    </step><step>
                        Weld all frame components together as shown in illustration <diag>3</diag></step><step>
                        Inspect all weld joints per Adventure Works Cycles Inspection Specification <specs>INFS-208</specs>.
                    </step></Location><Location LaborHours="1" LotSize="1" LocationID="30">
                    Work Center 30 - Debur and Polish. The following instructions pertain to Work Center 30. (Setup hours = 0, Labor Hours = 1, Machine Hours = 0, Lot Sizing = 1)<step>
                        Using the <tool>standard debur tool</tool>, remove all excess material from weld areas.
                    </step><step>
                        Using <material>Acme Polish Cream</material>, polish all weld areas.
                    </step></Location><Location LaborHours="0.5" LotSize="20" MachineHours="0.65" LocationID="45">
                    Work Center 45 - Specialized Paint. The following instructions pertain to Work Center 45. (Setup hours = 0, Labor Hours = .5, Machine Hours = .65, Lot Sizing = 20)<step>
                        Attach <material>a maximum of 20 frames</material> to <tool>paint harness</tool> ensuring frames are not touching.
                    </step><step>
                        Mix <material>primer PA-529S</material>. Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-501</specs>.
                    </step><step>Apply thin coat of primer to all surfaces.  </step><step>After 30 minutes, touch test for dryness. If dry to touch, lightly sand all surfaces. Remove all surface debris with compressed air. </step><step>
                        Mix <material>paint</material> per manufacturer instructions.
                    </step><step>
                        Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-509</specs>.
                    </step><step>Apply thin coat of paint to all surfaces. </step><step>After 60 minutes, touch test for dryness. If dry to touch, reapply second coat. </step><step>
                        Allow paint to cure for 24 hours and inspect per <specs>AWC-5015</specs>.
                    </step></Location><Location LaborHours="3" LotSize="1" SetupHours="0.25" LocationID="50">
                    Work Center 50 - SubAssembly. The following instructions pertain to Work Center 50. (Setup hours = .25, Labor Hours = 3, Machine Hours = 0, Lot Sizing = 1)<step>Add Seat Assembly. </step><step>Add Brake assembly.   </step><step>Add Wheel Assembly. </step><step>Inspect Front Derailleur. </step><step>Inspect Rear Derailleur. </step></Location><Location LaborHours="4" LotSize="1" LocationID="60">
                    Work Center 60 - Final Assembly. The following instructions pertain to Work Center 60. (Setup hours = 0, Labor Hours = 4, Machine Hours = 0, Lot Sizing = 1)<step>
                        Perform final inspection per engineering specification <specs>AWC-915</specs>.
                    </step><step>Complete all required certification forms.</step><step>Move to shipping.</step></Location></root>

我想输出为2.5。 我现在用的是下面提到query.However我收到错误

消息9314,级别16,状态1,2号线
的XQuery [Production.ProductModel.Instructions.value()]:无法隐式雾化或应用'FN:数据()'以复杂的内容元素,发现类型“{ http://schemas.microsoft.com/sqlserver/2004/07/冒险作品/ ProductModelManuInstructions }元素({“推断型内'位置http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions }:位置,#匿名)?'。

在下文提到的代码 -

with xmlnamespaces(default 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions')
select Instructions.value('(/root/Location)[1]','bigint')
from Production.ProductModel
where Instructions is not null

Answer 1:

你要什么属性或元素,从萃取<Location>的节点? 你不能把整个XML节点转换为bigint (如您的查询试图做的....)

你尝试提取<Location LaborHours="1.0">价值?

然后,你需要:

;WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions')
SELECT 
    XC.value('@LaborHours', 'decimal(18,4)')
FROM
    Production.ProductModel
CROSS APPLY
    Instructions.nodes('/root/Location') AS XT(XC)

首先:由于有多个 <Location>在你的XML节点,你必须使用CROSS APPLY.nodes()函数来获取所有的<Location> XML节点。

其次,由于要检索的属性 LaborHours (我的假设),您需要使用.value('@LaborHours )`方法。

最后,因为这些值是1.02.5 ,您必须使用decimal (适当精度和规模)来获取这些值-不bigint ....



文章来源: xquery value() method not working and is giving error