I want to get one xml node value from NTEXT column which contains xml based on where clause quering on another xml node value. RDBMS Type: Microsoft SQL Server T-SQL Here: I want to get Code node value based on StoreId where clause value. How do I get it? Input: 100 Output:ABCDE
我想从NTEXT列获得一个xml节点值,该列包含基于where子句对另一个xml节点值进行查询的xml。这里是Microsoft SQL Server T-SQL:我想根据StoreId where子值获取代码节点值。我怎么得到它?输入:100输出:中的
For example:
例如:
<root>
<StoreProfile>
<General>
<StoreId>100</StoreId>
<Code>ABCDE</Code>
</General>
</StoreProfile>
</root>
1 个解决方案
#1
6
If you are using SQL Server 2005 or 2008 you can use XQuery like so:
如果您正在使用SQL Server 2005或2008,您可以像这样使用XQuery:
For more on XQuery see XQuery Language Reference
有关XQuery的更多信息,请参见XQuery语言参考
DECLARE @storeId INT
SET @storeId = 100
CREATE TABLE #TestTable
(
xmlColumn NTEXT
)
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>100</StoreId><Code>ABCDE</Code></General></StoreProfile></root>')
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>200</StoreId><Code>FGHIJ</Code></General></StoreProfile></root>')
SELECT
StoreProfile.value('Code[1]', 'nvarchar(10)') as Code
FROM #TestTable
CROSS APPLY (SELECT CAST(xmlColumn AS XML)) AS A(B)
CROSS APPLY A.B.nodes('//root/StoreProfile/General[StoreId = sql:variable("@storeId")]') AS StoreProfiles(StoreProfile)
DROP TABLE #TestTable
#1
6
If you are using SQL Server 2005 or 2008 you can use XQuery like so:
如果您正在使用SQL Server 2005或2008,您可以像这样使用XQuery:
For more on XQuery see XQuery Language Reference
有关XQuery的更多信息,请参见XQuery语言参考
DECLARE @storeId INT
SET @storeId = 100
CREATE TABLE #TestTable
(
xmlColumn NTEXT
)
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>100</StoreId><Code>ABCDE</Code></General></StoreProfile></root>')
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>200</StoreId><Code>FGHIJ</Code></General></StoreProfile></root>')
SELECT
StoreProfile.value('Code[1]', 'nvarchar(10)') as Code
FROM #TestTable
CROSS APPLY (SELECT CAST(xmlColumn AS XML)) AS A(B)
CROSS APPLY A.B.nodes('//root/StoreProfile/General[StoreId = sql:variable("@storeId")]') AS StoreProfiles(StoreProfile)
DROP TABLE #TestTable