Chr*_*ian 3 xml sql t-sql xquery sql-server-2008
我试图插入一些xml我从表中的列中检索.我按如下方式检索它:
DECLARE @profiles_xml xml
DECLARE @profile_id int
SET @profile_id = 16
SET @profiles_xml = (SELECT profiles from tbl_applied_profiles WHERE
profiles.value('(Profile/ID)[1]','int')= @profile_id)
Run Code Online (Sandbox Code Playgroud)
生成的xml如下所示:
<Profile>
<ID>16</ID>
<User>
<ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
<Name>test</Name>
<Activities />
</User>
</Profile>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将元素插入其中时,如下所示:
SET @devices_xml.modify('insert <Activity><Name>testname</Name></Activity>
as first into (Profiles/User/Activities[1])')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
XQuery [modify()]: The target of 'insert' must be a single node, found 'element(Activities,xdt:untyped) *'
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会出现错误,因为目前只有一个Activities元素.
你有两个小错误:
<Profile>- 但你Profiles在插入XQuery中有[1]索引器放在错误的位置(在结束时)).试试这个XQuery:
SET @devices_xml.modify('insert <Activity><Name>testname</Name></Activity>
as first into (Profile/User/Activities)[1]')
Run Code Online (Sandbox Code Playgroud)
至少在我的情况下,然后返回:
<Profile>
<ID>16</ID>
<User>
<ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
<Name>test</Name>
<Activities>
<Activity>
<Name>testname</Name>
</Activity>
</Activities>
</User>
</Profile>
Run Code Online (Sandbox Code Playgroud)