涉及顺序的 XQuery
适用范围:SQL Server
关系数据库没有顺序的概念。 例如,无法发出请求,例如“从数据库获取第一个客户”。但是,可以查询 XML 文档并检索第一个 <Customer> 元素。 这样总是会检索到同一个客户。
本主题介绍了基于文档中节点的显示顺序的查询。
示例
A. 检索产品在第二个生产车间的生产步骤
以下查询针对某个特定产品型号,检索生产过程中一系列生产车间中第二个生产车间的生产步骤。
SELECT Instructions.query('
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
<ManuStep ProdModelID = "{sql:column("Production.ProductModel.ProductModelID")}"
ProductModelName = "{ sql:column("Production.ProductModel.Name") }" >
<Location>
{ (//AWMI:root/AWMI:Location)[2]/@* }
<Steps>
{ for $s in (//AWMI:root/AWMI:Location)[2]//AWMI:step
return
<Step>
{ string($s) }
</Step>
}
</Steps>
</Location>
</ManuStep>
') as Result
FROM Production.ProductModel
WHERE ProductModelID=7
请注意上述查询的以下方面:
大括号中的表达式替换为其计算结果。 有关详细信息,请参阅 XML 构造(XQuery)。
@* 检索第二个工作中心位置的所有属性。
FLWOR 迭代 (FOR ...RETURN) 检索第二个工作中心位置的所有 <
step
> 子元素。sql:column() 函数 (XQuery) 在正在构造的 XML 中包含关系值。
结果如下:
<ManuStep ProdModelID="7" ProductModelName="HL Touring Frame">
<Location LocationID="20" SetupHours="0.15"
MachineHours="2" LaborHours="1.75" LotSize="1">
<Steps>
<Step>Assemble all frame components following blueprint 1299.</Step>
...
</Steps>
</Location>
</ManuStep>
上述查询只检索文本节点。 如果希望返回整个 <step
> 元素,请从查询中删除 string() 函数:
B. 查找产品生产过程中在第二个生产车间使用的所有材料和工具
以下查询针对某个特定产品型号,检索生产过程中一系列生产车间中第二个生产车间使用的工具和材料。
SELECT Instructions.query('
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
<Location>
{ (//AWMI:root/AWMI:Location)[1]/@* }
<Tools>
{ for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:tool
return
<Tool>
{ string($s) }
</Tool>
}
</Tools>
<Materials>
{ for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:material
return
<Material>
{ string($s) }
</Material>
}
</Materials>
</Location>
') as Result
FROM Production.ProductModel
where ProductModelID=7
请注意上述查询的以下方面:
查询构造 <Loca
tion
> 元素,并从数据库中检索其属性值。它使用了两个 FLWOR (for...return) 迭代:一个用于检索工具,一个用于检索使用的材料。
结果如下:
<Location LocationID="10" SetupHours=".5"
MachineHours="3" LaborHours="2.5" LotSize="100">
<Tools>
<Tool>T-85A framing tool</Tool>
<Tool>Trim Jig TJ-26</Tool>
<Tool>router with a carbide tip 15</Tool>
<Tool>Forming Tool FT-15</Tool>
</Tools>
<Materials>
<Material>aluminum sheet MS-2341</Material>
</Materials>
</Location>
°C 从产品目录中检索前两个产品的功能说明
对于特定产品模型,查询从 <Features
> 产品模型目录中的元素检索前两个功能说明。
SELECT CatalogDescription.query('
declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
<ProductModel ProductModelID= "{ data( (/p1:ProductDescription/@ProductModelID)[1] ) }"
ProductModelName = "{ data( (/p1:ProductDescription/@ProductModelName)[1] ) }" >
{
for $F in /p1:ProductDescription/p1:Features
return
$F/*[position() <= 2]
}
</ProductModel>
') as x
FROM Production.ProductModel
where ProductModelID=19
请注意上述查询的以下方面:
查询正文构造包含 ProductModelID 和 ProductModelName 属性的元素的 XML <ProductModel
> 。
- 查询使用 FOR...RETURN 循环,用于检索产品模型功能说明。 position() 函数用于检索前两个特征。
结果如下:
<ProductModel ProductModelID="19" ProductModelName="Mountain 100">
<p1:Warranty
xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
<p1:WarrantyPeriod>3 year</p1:WarrantyPeriod>
<p1:Description>parts and labor</p1:Description>
</p1:Warranty>
<p2:Maintenance
xmlns:p2="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
<p2:NoOfYears>10</p2:NoOfYears>
<p2:Description>maintenance contact available through your dealer
or any AdventureWorks retail store.
</p2:Description>
</p2:Maintenance>
</ProductModel>
D. 查找产品生产过程中在第一个生产车间使用的前两个工具
此查询针对某个产品型号,返回生产过程中一系列生产车间中第一个生产车间使用的前两个工具。 针对 Production.ProductModel 表的“指令”列中存储的制造指令指定查询。
SELECT Instructions.query('
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
for $Inst in (//AWMI:root/AWMI:Location)[1]
return
<Location>
{ $Inst/@* }
<Tools>
{ for $s in ($Inst//AWMI:step//AWMI:tool)[position() <= 2]
return
<Tool>
{ string($s) }
</Tool>
}
</Tools>
</Location>
') as Result
FROM Production.ProductModel
where ProductModelID=7
结果如下:
<Location LocationID="10" SetupHours=".5"
MachineHours="3" LaborHours="2.5" LotSize="100">
<Tools>
<Tool>T-85A framing tool</Tool>
<Tool>Trim Jig TJ-26</Tool>
</Tools>
</Location>
E. 查找某个特定产品生产过程中在第一个生产车间中的最后两个生产步骤
查询使用 last() 函数检索最后两个制造步骤。
SELECT Instructions.query('
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
<LastTwoManuSteps>
<Last-1Step>
{ (/AWMI:root/AWMI:Location)[1]/AWMI:step[(last()-1)]/text() }
</Last-1Step>
<LastStep>
{ (/AWMI:root/AWMI:Location)[1]/AWMI:step[last()]/text() }
</LastStep>
</LastTwoManuSteps>') as Result
FROM Production.ProductModel
where ProductModelID=7
结果如下:
<LastTwoManuSteps>
<Last-1Step>When finished, inspect the forms for defects per
Inspection Specification .</Last-1Step>
<LastStep>Remove the frames from the tool and place them in the
Completed or Rejected bin as appropriate.</LastStep>
</LastTwoManuSteps>
另请参阅
XML 数据 (SQL Server)
Xquery 语言参考 (SQL Server)
XML 构造 (XQuery)