0

i just need your help. I was looking for a solution, but nothing works yet.

I have to select multiple attributes from the xml-file which is stored in a column of my table.

This is the File:

<ManagerConfig>
   <AccountList>
     <Account accountID=“1“  friendlyName=“Testname1“&gt; Test </Account>
     <Account accountID=“2“  friendlyName=“Testname2“&gt; Test </Account>
     <Account accountID=“3“  friendlyName=“Testname3“&gt; Test </Account>
     <Account accountID=“4“  friendlyName=“Testname4“&gt; Test </Account>
   </AccountList
</ManagerConfig>

For this I´m using the following statement:

set @accountID = @xmlxx.value('(/ManagerConfig/AccountList/Account/@accountId)[1]', 'varchar(max)')
set @friendlyName = @xmlxx.value('(/ManagerConfig/AccountList/Account/@friendlyName)[1]', 'varchar(max)')

The result is:

accountID     friendlyname
1             Testname1

When im changing the value from [1] to [2] im getting the second attribute. So thats no problem. But i need all of these attributes and export them into another temporary table. I thought i can replace the value with a variable [@i]:

set @accountID = @xmlxx.value('(/(ManagerConfig/AccountList/Account/@accountId)'[@i]'', 'varchar(max)')

But there is a syntax error:

An insufficient number of arguments were supplied for the procedure or function value.

I hope you can help me to find a solution..

Greetz Dennis

4

1 回答 1

2

假设您要过渡到获取结果集(可以包含多个结果),而不是当前使用变量,例如:

select t.C.value('@accountID','int') as AccountID,t.C.value('@friendlyName','varchar(max)') as FriendlyName
from @xmlxx.nodes('/ManagerConfig/AccountList/Account') as t(C)

(原始设置和测试脚本,清除有问题的奇怪格式,并更正Id-> ID,这可能是错误的修复方向):

declare @xmlxx xml = '<ManagerConfig>
   <AccountList>
     <Account accountID="1"  friendlyName="Testname1"> Test </Account>
     <Account accountID="2"  friendlyName="Testname2"> Test </Account>
     <Account accountID="3"  friendlyName="Testname3"> Test </Account>
     <Account accountID="4"  friendlyName="Testname4"> Test </Account>
   </AccountList>
</ManagerConfig>'
declare @accountID varchar(max)
declare @friendlyName varchar(max)
set @accountID = @xmlxx.value('(/ManagerConfig/AccountList/Account/@accountID)[1]', 'varchar(max)')
set @friendlyName = @xmlxx.value('(/ManagerConfig/AccountList/Account/@friendlyName)[1]', 'varchar(max)')
于 2012-06-19T13:08:31.600 回答