如何根据 SQL 中的条件 select xml 数据
How to select xml data based on a condition in SQL
下面是我的xml
DECLARE @myDoc xml
DECLARE @ProdID int
SET @myDoc =
'<Root>
<ProductDescription>
<ProductID>1</ProductID>
<ProductName>Road Bike</ProductName>
<GenID>0C866AE2-7AAA-474F-8794-7538986268AE</GenID>
<VocID>AF05E961-9BC3-4249-A4A7-C6146D6FC614</VocID>
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
<ProductDescription>
<ProductID>2</ProductID>
<ProductName>Road Bike2</ProductName>
<GenID>D1DCAD29-08C6-401E-9A0A-2130DC5D8CD4</GenID>
<VocID>AF05E961-9BC3-4249-A4A7-C6146D6FC614</VocID>
<Features>
<Warranty>2 year parts and labor</Warranty>
<Maintenance>4 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
SET @ProdID = @myDoc.value('(/Root/ProductDescription/ProductID)[2]', 'int' )
SELECT @ProdID
这将为我提供第二项的 ID。如果我改变
我有 GenID
和 VocID
。使用它我需要查询并获取其他数据。
请帮助我使用 SQL Server 2012 为 same.I 生成查询。提前致谢。
我会 select 直接从 XML 数据,只需将您正在 selecting 的项目编号移动到 nodes
而不是 selecting一个 ID,然后按该 ID 过滤。这样您就已经可以访问其他信息了。
例如
SELECT ProductID = x.value('ProductID[1]', 'int'),
ProductName = x.value('ProductName[1]', 'varchar(100)'),
GenID = x.value('GenID[1]', 'uniqueidentifier')
FROM @myDoc.nodes('/Root/ProductDescription[2]') prod (x);
编辑
要获取与给定 GenID 和 VocID 相匹配的记录,您可以使用 exist()
方法:
DECLARE @VocID UNIQUEIDENTIFIER = 'AF05E961-9BC3-4249-A4A7-C6146D6FC614',
@GenID UNIQUEIDENTIFIER = 'D1DCAD29-08C6-401E-9A0A-2130DC5D8CD4';
SELECT ProductID = x.value('ProductID[1]', 'int'),
ProductName = x.value('ProductName[1]', 'varchar(100)'),
GenID = x.value('GenID[1]', 'uniqueidentifier')
FROM @myDoc.nodes('/Root/ProductDescription') prod (x)
WHERE x.exist('VocID[text() = sql:variable("@VocID")]') = 1
AND x.exist('GenID[text() = sql:variable("@GenID")]') = 1;
下面是我的xml
DECLARE @myDoc xml
DECLARE @ProdID int
SET @myDoc =
'<Root>
<ProductDescription>
<ProductID>1</ProductID>
<ProductName>Road Bike</ProductName>
<GenID>0C866AE2-7AAA-474F-8794-7538986268AE</GenID>
<VocID>AF05E961-9BC3-4249-A4A7-C6146D6FC614</VocID>
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
<ProductDescription>
<ProductID>2</ProductID>
<ProductName>Road Bike2</ProductName>
<GenID>D1DCAD29-08C6-401E-9A0A-2130DC5D8CD4</GenID>
<VocID>AF05E961-9BC3-4249-A4A7-C6146D6FC614</VocID>
<Features>
<Warranty>2 year parts and labor</Warranty>
<Maintenance>4 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
SET @ProdID = @myDoc.value('(/Root/ProductDescription/ProductID)[2]', 'int' )
SELECT @ProdID
这将为我提供第二项的 ID。如果我改变
我有 GenID
和 VocID
。使用它我需要查询并获取其他数据。
请帮助我使用 SQL Server 2012 为 same.I 生成查询。提前致谢。
我会 select 直接从 XML 数据,只需将您正在 selecting 的项目编号移动到 nodes
而不是 selecting一个 ID,然后按该 ID 过滤。这样您就已经可以访问其他信息了。
例如
SELECT ProductID = x.value('ProductID[1]', 'int'),
ProductName = x.value('ProductName[1]', 'varchar(100)'),
GenID = x.value('GenID[1]', 'uniqueidentifier')
FROM @myDoc.nodes('/Root/ProductDescription[2]') prod (x);
编辑
要获取与给定 GenID 和 VocID 相匹配的记录,您可以使用 exist()
方法:
DECLARE @VocID UNIQUEIDENTIFIER = 'AF05E961-9BC3-4249-A4A7-C6146D6FC614',
@GenID UNIQUEIDENTIFIER = 'D1DCAD29-08C6-401E-9A0A-2130DC5D8CD4';
SELECT ProductID = x.value('ProductID[1]', 'int'),
ProductName = x.value('ProductName[1]', 'varchar(100)'),
GenID = x.value('GenID[1]', 'uniqueidentifier')
FROM @myDoc.nodes('/Root/ProductDescription') prod (x)
WHERE x.exist('VocID[text() = sql:variable("@VocID")]') = 1
AND x.exist('GenID[text() = sql:variable("@GenID")]') = 1;