我试图修改第三方xml,以便所有元素都有来自T-SQL的ID。
这是原始的xml(部分):
<Tables>
<Table Type="LineItem">
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">244234</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">RENT RECEIVABLE - INTERNAL</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">4522.89</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">56666</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">150021</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">Rent Building 1</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">456.37</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">2805.00</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">2342341</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">Rent Building 2</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">355</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">6900</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
</Table>
<Table Type="BankAccountTable" />
<Table Type="VATTable" />
</Tables>
正如您所看到的,<Table>
元素没有ID,因此在此过程中很难识别它们。
我想创建一个循环来遍历所有<Table>
元素并执行这样的代码段:
set @xml.modify('
insert attribute ID {sql:variable("@idString")}
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[sql:variable("@id")]')
问题在于最后一个sql变量,错误是:
XQuery [modify()]:只有&#39; http://www.w3.org/2001/XMLSchema#decimal?&#39;, &#39; HTTP://www.w3.org/2001/XMLSchema#boolean&#39;或者&#39;节点()*&#39;表达式 允许作为谓词,找到&#39; xs:string?&#39;
这很好用,但我不想总是改变同一行(数字1)。
set @xml.modify('
insert attribute ID {sql:variable("@idString")}
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[1]')
顺便说一句,如果我使用int变量而不是字符串:
set @xml.modify('insert attribute ID {sql:variable("@idString")}
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[sql:variable("@idInt")]')`
我收到另一个错误:
XQuery [modify()]:&#39;插入&#39;的目标找到的必须是单个节点 &#39;元素(TableRow,xdt:untyped)*
答案 0 :(得分:2)
您可以将变量@id
与函数position()
进行比较。
declare @id int
declare @RowCount int
select @RowCount = @xml.value('count(/Tables/Table[@Type="LineItem"]/TableRow)', 'int')
set @id = 1
while @id <= @RowCount
begin
set @xml.modify('
insert attribute ID {sql:variable("@id")}
into (/Tables/Table[@Type="LineItem"]/TableRow[position()=sql:variable("@id")])[1]')
set @id = @id + 1
end
答案 1 :(得分:1)
我能想到的唯一一件事是存储过程中包含所有可能的行(听说有人冲厕所?)
if (@row = 1)
BEGIN
set @xml.modify('
insert attribute ID {sql:variable("@id")}
into (/Documents[1]/Document[1]/Invoice[1]/Tables[1]/Table[@Type="LineItem"][1]/TableRow)[1]
')
END
ELSE if (@row = 2)
BEGIN
set @xml.modify('
insert attribute ID {sql:variable("@id")}
into (/Documents[1]/Document[1]/Invoice[1]/Tables[1]/Table[@Type="LineItem"][1]/TableRow)[2]
')
END
ELSE if (@row = 3)
BEGIN
set @xml.modify('
insert attribute ID {sql:variable("@id")}
into (/Documents[1]/Document[1]/Invoice[1]/Tables[1]/Table[@Type="LineItem"][1]/TableRow)[3]
')
END
希望不会超过200 ......