此sql仅返回第一个Activity元素.我如何选择它们?如果我删除查询中的[1],我会收到“value()需要单例”的错误.
DECLARE @myDoc xml SET @myDoc = '<Root> <Activities> <Activity>This is activity one</Activity> <Activity>This is activity two</Activity> <Activity>This is activity three</Activity> </Activities> </Root>' SELECT @myDoc.value('(/Root/Activities/Activity)[1]','varchar(100)' )
解决方法
谢谢Ed,但我找到了一个更简单的版本:
SELECT T.C.value('.','varchar(100)') as activity FROM @myDoc.nodes('(/Root/Activities/Activity)') as T(C)
虽然从你的“不必要的复杂”例子看起来令人担忧的简单……