Adding data from the xml into a SQL Server database in Biztalk

zzoitvuj  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(124)

I have a XML in the format below with fields company and account etc, which would be the source schema

<Root xmlns="http://schema.Balance_SourceSchema">
    <Balance xmlns="">
        <Company>"PKG"</Company>
        <Account>"1010.10.10"</Account>
        <Type>"P&L"</Type>
        <Description>"FREIGHT REVENUE"</Description>
        <Units>""</Units>
        <Branch>"HKC"</Branch>
        <Department>"BIL"</Department>
        <Activity>"Forwarding"</Activity>
        <Direction>"Export"</Direction>
        <Mode>"Sea"</Mode>
        <Year>"2023"</Year>
        <Period>"202305"</Period>
        <AmountInPeriod>"-31,844.20</AmountInPeriod>
        <Currency>"HAKD"</Currency>
    </Balance> ...

And the table should have the same fields + ID, Created Date added to it.

t3psigkw

t3psigkw1#

Please try the following as a starting point for your stored procedure.

You may need to adjust a couple of things:

  • Modify .value() method 2nd parameter data types to match your target table.
  • Remove surrounding double quotes via TRIM() function.

SQL

DECLARE @param XML = 
N'<Root xmlns="http://schema.Balance_SourceSchema">
    <Balance xmlns="">
        <Company>"PKG"</Company>
        <Account>"1010.10.10"</Account>
        <Type>"P&amp;L"</Type>
        <Description>"FREIGHT REVENUE"</Description>
        <Units>""</Units>
        <Branch>"HKC"</Branch>
        <Department>"BIL"</Department>
        <Activity>"Forwarding"</Activity>
        <Direction>"Export"</Direction>
        <Mode>"Sea"</Mode>
        <Year>"2023"</Year>
        <Period>"202305"</Period>
        <AmountInPeriod>"-31,844.20</AmountInPeriod>
        <Currency>"HAKD"</Currency>
    </Balance>
</Root>';
    
;WITH XMLNAMESPACES('http://schema.Balance_SourceSchema' AS ns1)
--INSERT INTO <targetTable> (Company, Account, ...)
SELECT TRIM('"' FROM c.value('(Company/text())[1]', 'VARCHAR(20)')) AS Company
, c.value('(Account/text())[1]', 'VARCHAR(20)') AS Account
, c.value('(Type/text())[1]', 'VARCHAR(20)') AS Type
, c.value('(Description/text())[1]', 'VARCHAR(50)') AS Description
, c.value('(Units/text())[1]', 'VARCHAR(20)') AS Units
, c.value('(Branch/text())[1]', 'VARCHAR(20)') AS Branch
, c.value('(Department/text())[1]', 'VARCHAR(20)') AS Department
, c.value('(Activity/text())[1]', 'VARCHAR(20)') AS Activity
, c.value('(Direction/text())[1]', 'VARCHAR(20)') AS Direction
, c.value('(Mode/text())[1]', 'VARCHAR(20)') AS Mode
, c.value('(Year/text())[1]', 'CHAR(4)') AS Year
, c.value('(Period/text())[1]', 'VARCHAR(10)') AS Period
, c.value('(AmountInPeriod/text())[1]', 'VARCHAR(20)') AS AmountInPeriod
, c.value('(Currency/text())[1]', 'VARCHAR(20)') AS Currency
FROM @param.nodes('/ns1:Root/Balance') AS t(c);

相关问题