Многострочный XML возврат в mssql - PullRequest
0 голосов
/ 17 июня 2020

У меня есть XML, возвращенные приложением. Я пишу запрос для вставки всех сумм из XML в таблицу и ожидаю, что значения вернутся как 100, 99,09, но из-за второй строки с тем же именем вкладки 0 добавляется к сумме, которая возвращает мне 1000 вместо 100. Как решить эту проблему?

declare @s xml
set @s='<Transaction><Header><HeaderId>42</HeaderId><ProdOrderId>0</ProdOrderId><VoucherType>8448</VoucherType><DocNo>1</DocNo><Date>132384273</Date><Time>0</Time><CreatedDate>132384273</CreatedDate><CreatedTime>932124</CreatedTime><ModifiedDate>132384273</ModifiedDate><ModifiedTime>932124</ModifiedTime><Flags><UpdateInv>false</UpdateInv><UpdateFA>true</UpdateFA><CantEdit>false</CantEdit><Editing>true</Editing><FromWeb>false</FromWeb><Suspended>false</Suspended><Internal>false</Internal><Approved>true</Approved><PrintAfterSave>false</PrintAfterSave><UnsaveInv>false</UnsaveInv><RequestCrLimit>false</RequestCrLimit><SaveInThread>false</SaveInThread><TDSCertPrepared>false</TDSCertPrepared><TDSPaid>false</TDSPaid><PostCashEntry>false</PostCashEntry><FromTrigger>false</FromTrigger><CheckNegativeCash>true</CheckNegativeCash><CheckCreditLimit>true</CheckCreditLimit><CheckOverdueBills>true</CheckOverdueBills><CheckNegativeStock>true</CheckNegativeStock><CheckNegativeBudget>true</CheckNegativeBudget><PostingFromUI>false</PostingFromUI><CheckReorderLevel>true</CheckReorderLevel><Cancelled>false</Cancelled><AuthByHigherUps>false</AuthByHigherUps><CantPrint>false</CantPrint><Version>false</Version><FromUI>false</FromUI><NoLinkCheck>true</NoLinkCheck><NoBatchCheck>true</NoBatchCheck><FromPDC>false</FromPDC><AlreadyLoaded>false</AlreadyLoaded><WMSAllocated>false</WMSAllocated><Amended>false</Amended><DontAdjustBills>false</DontAdjustBills><ChequeReturn>false</ChequeReturn><TriggerRaised>false</TriggerRaised></Flags><EnteredBy>1</EnteredBy><ModifiedBy>1</ModifiedBy><LC>0</LC><PmtTerm>0</PmtTerm><PrintCount>0</PrintCount><ModifiedDiffLoc>false</ModifiedDiffLoc><EditingLocation>0</EditingLocation><SyncDateTime>0</SyncDateTime><ProductionSize>0</ProductionSize><Versions /><BillStatus>None</BillStatus><LinkStatus>Partial</LinkStatus><TriggerBaseHeaderId>0</TriggerBaseHeaderId><EmailCount>0</EmailCount><Source>Screen</Source><ProcessId>0</ProcessId><Net>0.0000000000</Net><TransNet>0</TransNet><RepostId>0</RepostId><AuthStatus>Authorized</AuthStatus></Header><HeaderExtra><IdNamePair><ID>67108916</ID><Name>sNarration</Name><Tag></Tag></IdNamePair></HeaderExtra><BodyData><TransBody><TransactionId>363</TransactionId><BodyId>363</BodyId><Sequence>0</Sequence><FATag>11</FATag><InvTag>0</InvTag><Code>4</Code><Book>65</Book><CurrencyId>7</CurrencyId><ExchangeRate>1.0000000000</ExchangeRate><LocalExchangeRate>0</LocalExchangeRate><Tags 
/><BodyExtra><IdNamePair><ID>16779281</ID><Name>ExchngDiff</Name><Tag>5.120</Tag></IdNamePair></BodyExtra><Amounts><decimal>-100</decimal><decimal>0</decimal></Amounts><OriginalAmounts><decimal>-100</decimal><decimal>0</decimal></OriginalAmounts><Amount3>0</Amount3><TransAmount3>0</TransAmount3><OrigAmount>0</OrigAmount><DueDate>132384273</DueDate><RowType>Default</RowType><AuthStatus>Authorized</AuthStatus><Flags><SuspendFA>false</SuspendFA><SuspendInv>false</SuspendInv><SuspendBase>false</SuspendBase><SuspendLink>false</SuspendLink><Void>false</Void><ForexFlux>false</ForexFlux><BRS>false</BRS><PDC>false</PDC><TransferToPnL>false</TransferToPnL><InternalIIDST>false</InternalIIDST><ConfirmBins>false</ConfirmBins><FreeQty>false</FreeQty><PDCDisc>false</PDCDisc><SuspendRef>false</SuspendRef><SuspendReservation>false</SuspendReservation><FooterRow>false</FooterRow><PDCConverted>false</PDCConverted><UpdateFA>true</UpdateFA></Flags><LocalCurrencyData><CurrencyId>0</CurrencyId><ExchangeRate>1</ExchangeRate><Amounts><decimal>0.0000000000</decimal><decimal>0.0000000000</decimal></Amounts><Amount3>0</Amount3></LocalCurrencyData><MainBodyId>0</MainBodyId></TransBody><TransBody><TransactionId>364</TransactionId><BodyId>364</BodyId><Sequence>0</Sequence><FATag>11</FATag><InvTag>0</InvTag><Code>62</Code><Book>65</Book><CurrencyId>2</CurrencyId><ExchangeRate>3.6700000000</ExchangeRate><LocalExchangeRate>0</LocalExchangeRate><Tags 
/><BodyExtra><IdNamePair><ID>16779281</ID><Name>ExchngDiff</Name><Tag>0</Tag></IdNamePair></BodyExtra>
<Amounts><decimal>99.09</decimal></Amounts><OriginalAmounts><decimal>27.00</decimal><decimal>0</decimal></OriginalAmounts><Amount3>0</Amount3><TransAmount3>0</TransAmount3><OrigAmount>0</OrigAmount><DueDate>132384273</DueDate><RowType>Default</RowType><AuthStatus>Authorized</AuthStatus><Flags><SuspendFA>false</SuspendFA><SuspendInv>false</SuspendInv><SuspendBase>false</SuspendBase><SuspendLink>false</SuspendLink><Void>false</Void><ForexFlux>false</ForexFlux><BRS>false</BRS><PDC>false</PDC><TransferToPnL>false</TransferToPnL><InternalIIDST>false</InternalIIDST><ConfirmBins>false</ConfirmBins><FreeQty>false</FreeQty><PDCDisc>false</PDCDisc><SuspendRef>false</SuspendRef><SuspendReservation>false</SuspendReservation><FooterRow>false</FooterRow><PDCConverted>false</PDCConverted><UpdateFA>true</UpdateFA></Flags><LocalCurrencyData><CurrencyId>0</CurrencyId><ExchangeRate>1</ExchangeRate><Amounts><decimal>0.0000000000</decimal><decimal>0.0000000000</decimal></Amounts><Amount3>0</Amount3></LocalCurrencyData><MainBodyId>0</MainBodyId></TransBody></BodyData></Transaction>'
declare @table table (Amount1 decimal(18,3))
insert into @table 
select Node.Data.value('Amounts[1]', 'decimal(18,3)') as Amount
from @s.nodes('Transaction/BodyData/TransBody') Node(data)
select * from @table

1 Ответ

0 голосов
/ 17 июня 2020

Попробуйте следующий T- SQL.

SQL

-- DDL and sample data population, start
DECLARE @s XML =
N'<Transaction>
    <BodyData>
        <TransBody>
            <TransactionId>363</TransactionId>
            <BodyId>363</BodyId>
            <Sequence>0</Sequence>
            <FATag>11</FATag>
            <InvTag>0</InvTag>
            <Code>4</Code>
            <Book>65</Book>
            <CurrencyId>7</CurrencyId>
            <ExchangeRate>1.0000000000</ExchangeRate>
            <LocalExchangeRate>0</LocalExchangeRate>
            <Tags/>
            <BodyExtra>
                <IdNamePair>
                    <ID>16779281</ID>
                    <Name>ExchngDiff</Name>
                    <Tag>5.120</Tag>
                </IdNamePair>
            </BodyExtra>
            <Amounts>
                <decimal>-100</decimal>
                <decimal>0</decimal>
            </Amounts>
            <OriginalAmounts>
                <decimal>-100</decimal>
                <decimal>0</decimal>
            </OriginalAmounts>
            <Amount3>0</Amount3>
            <TransAmount3>0</TransAmount3>
            <OrigAmount>0</OrigAmount>
            <DueDate>132384273</DueDate>
            <RowType>Default</RowType>
            <AuthStatus>Authorized</AuthStatus>
            <Flags>
                <SuspendFA>false</SuspendFA>
                <SuspendInv>false</SuspendInv>
                <SuspendBase>false</SuspendBase>
                <SuspendLink>false</SuspendLink>
                <Void>false</Void>
                <ForexFlux>false</ForexFlux>
                <BRS>false</BRS>
                <PDC>false</PDC>
                <TransferToPnL>false</TransferToPnL>
                <InternalIIDST>false</InternalIIDST>
                <ConfirmBins>false</ConfirmBins>
                <FreeQty>false</FreeQty>
                <PDCDisc>false</PDCDisc>
                <SuspendRef>false</SuspendRef>
                <SuspendReservation>false</SuspendReservation>
                <FooterRow>false</FooterRow>
                <PDCConverted>false</PDCConverted>
                <UpdateFA>true</UpdateFA>
            </Flags>
            <LocalCurrencyData>
                <CurrencyId>0</CurrencyId>
                <ExchangeRate>1</ExchangeRate>
                <Amounts>
                    <decimal>0.0000000000</decimal>
                    <decimal>0.0000000000</decimal>
                </Amounts>
                <Amount3>0</Amount3>
            </LocalCurrencyData>
            <MainBodyId>0</MainBodyId>
        </TransBody>
        <TransBody>
            <TransactionId>364</TransactionId>
            <BodyId>364</BodyId>
            <Sequence>0</Sequence>
            <FATag>11</FATag>
            <InvTag>0</InvTag>
            <Code>62</Code>
            <Book>65</Book>
            <CurrencyId>2</CurrencyId>
            <ExchangeRate>3.6700000000</ExchangeRate>
            <LocalExchangeRate>0</LocalExchangeRate>
            <Tags/>
            <BodyExtra>
                <IdNamePair>
                    <ID>16779281</ID>
                    <Name>ExchngDiff</Name>
                    <Tag>0</Tag>
                </IdNamePair>
            </BodyExtra>
            <Amounts>
                <decimal>99.09</decimal>
            </Amounts>
            <OriginalAmounts>
                <decimal>27.00</decimal>
                <decimal>0</decimal>
            </OriginalAmounts>
            <Amount3>0</Amount3>
            <TransAmount3>0</TransAmount3>
            <OrigAmount>0</OrigAmount>
            <DueDate>132384273</DueDate>
            <RowType>Default</RowType>
            <AuthStatus>Authorized</AuthStatus>
            <Flags>
                <SuspendFA>false</SuspendFA>
                <SuspendInv>false</SuspendInv>
                <SuspendBase>false</SuspendBase>
                <SuspendLink>false</SuspendLink>
                <Void>false</Void>
                <ForexFlux>false</ForexFlux>
                <BRS>false</BRS>
                <PDC>false</PDC>
                <TransferToPnL>false</TransferToPnL>
                <InternalIIDST>false</InternalIIDST>
                <ConfirmBins>false</ConfirmBins>
                <FreeQty>false</FreeQty>
                <PDCDisc>false</PDCDisc>
                <SuspendRef>false</SuspendRef>
                <SuspendReservation>false</SuspendReservation>
                <FooterRow>false</FooterRow>
                <PDCConverted>false</PDCConverted>
                <UpdateFA>true</UpdateFA>
            </Flags>
            <LocalCurrencyData>
                <CurrencyId>0</CurrencyId>
                <ExchangeRate>1</ExchangeRate>
                <Amounts>
                    <decimal>0.0000000000</decimal>
                    <decimal>0.0000000000</decimal>
                </Amounts>
                <Amount3>0</Amount3>
            </LocalCurrencyData>
            <MainBodyId>0</MainBodyId>
        </TransBody>
    </BodyData>
</Transaction>';

DECLARE @table TABLE (ID INT IDENTITY PRIMARY KEY, Amount DECIMAL(18, 3));
-- DDL and sample data population, end

INSERT INTO @table
SELECT c.value('(./text())[1]', 'DECIMAL(18,2)') AS Amount
FROM @s.nodes('/Transaction/BodyData/TransBody/Amounts/decimal') t(c);

-- test
SELECT * FROM @table;

Выход

+----+----------+
| ID |  Amount  |
+----+----------+
|  1 | -100.000 |
|  2 |    0.000 |
|  3 |   99.090 |
+----+----------+
...