简体   繁体   English

SQL XML选择多个属性

[英]SQL XML select multiple attributes

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. 我必须从存储在表列中的xml文件中选择多个属性。

This is the File: 这是文件:

<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>

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. 当我将值从[1]更改为[2]时,我获得了第二个属性。 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]: 我以为我可以用变量[@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 格蕾兹·丹尼斯(Greetz Dennis)

Assuming you want to transition to getting a result set (that can contain multiple results), rather than your current use of a variable, something like: 假设您要转换为获取一个结果集(可以包含多个结果),而不是当前使用的变量,例如:

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)

(Original setup and test script, cleaning from odd formatting in question, and correcting Id -> ID , which may be the wrong direction for the fix): (原始设置和测试脚本,从所讨论奇数格式化清洁和校正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)')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM