你可以做
SET @xmlData.modify('insert attribute runat { "server" } into descendant::xxx[not(@runat)][1]');但是,这只会更改
xxx不具有runat属性的第一个后代,至少对于SQL Server 2005,您一次只能修改一个节点。
也许将以上内容与WHILE结合使用,例如
WHILE @xmlData.exist('descendant::xxx[not(@runat)]') = 1BEGIN SET @xmlData.modify('insert attribute runat { "server" } into descendant::xxx[not(@runat)][1]');END我没有访问SQL Server 2008 R2的权限,但我认为修改一次仍仅限于一个节点,因此您可以尝试
ALTER FUNCTION [dbo].[ConvertXmlData](@xmlData XML)RETURNS XMLASBEGIN WHILE @xmlData.exist('descendant::xxx[not(@runat)]') = 1 BEGIN SET @xmlData.modify('insert attribute runat { "server" } into descendant::xxx[not(@runat)][1]'); END RETURN @xmlDataEND


