【问题标题】:Get data XML file from web link and show on SQL Server table [closed]从 Web 链接获取数据 XML 文件并显示在 SQL Server 表上 [关闭]
【发布时间】:2021-12-26 13:04:27
【问题描述】:

我有这个链接https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml,我想在 SQL Server 表中放入两列,如货币和汇率,如链接所示。

我该怎么做?

感谢您的帮助。

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
    <gesmes:subject>Reference rates</gesmes:subject>
    <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time='2021-11-12'>
            <Cube currency='USD' rate='1.1448'/>
            <Cube currency='JPY' rate='130.50'/>
            <Cube currency='BGN' rate='1.9558'/>
            <Cube currency='CZK' rate='25.238'/>
            <Cube currency='DKK' rate='7.4370'/>
            <Cube currency='GBP' rate='0.85505'/>
            <Cube currency='HUF' rate='366.15'/>
            <Cube currency='PLN' rate='4.6428'/>
            <Cube currency='RON' rate='4.9488'/>
            <Cube currency='SEK' rate='10.0085'/>
            <Cube currency='CHF' rate='1.0568'/>
            <Cube currency='ISK' rate='150.40'/>
            <Cube currency='NOK' rate='9.9508'/>
            <Cube currency='HRK' rate='7.5123'/>
            <Cube currency='RUB' rate='82.6649'/>
            <Cube currency='TRY' rate='11.3987'/>
            <Cube currency='AUD' rate='1.5690'/>
            <Cube currency='BRL' rate='6.1902'/>
            <Cube currency='CAD' rate='1.4416'/>
            <Cube currency='CNY' rate='7.3047'/>
            <Cube currency='HKD' rate='8.9206'/>
            <Cube currency='IDR' rate='16239.91'/>
            <Cube currency='ILS' rate='3.5600'/>
            <Cube currency='INR' rate='85.1930'/>
            <Cube currency='KRW' rate='1349.10'/>
            <Cube currency='MXN' rate='23.6472'/>
            <Cube currency='MYR' rate='4.7692'/>
            <Cube currency='NZD' rate='1.6293'/>
            <Cube currency='PHP' rate='56.995'/>
            <Cube currency='SGD' rate='1.5494'/>
            <Cube currency='THB' rate='37.527'/>
            <Cube currency='ZAR' rate='17.4919'/>
        </Cube>
    </Cube>
</gesmes:Envelope>

【问题讨论】:

  • 这里的问题应该是自包含的。 IE。外部链接还不够,向我们展示一些示例表数据和格式化文本的预期结果。尽可能简化,即minimal reproducible example.
  • 如果没有链接(正如所解释的,不应该是链接,而应该是内容in)你的问题,你的问题实际上变成了“我想在 SqlTable 中放入货币和汇率之类的两列。我该怎么做?” 这很不清楚。
  • @jarlh 已添加相关XML
  • OP 应该真的做到这一点,@Charlieface。 ;)

标签: sql sql-server xml


【解决方案1】:

看起来你需要这样的东西:

WITH
  DEFAULT 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref',
  'http://www.gesmes.org/xml/2002-08-01' AS gesmes)
INSERT YourRateTable (Currency, Rate)
SELECT
  x.cube.value('@currency','char(3)'),
  x.cube.value('@rate','decimal(18,9)')
FROM @xml.nodes('/gesmes:Envelope/Cube/Cube/Cube') x(cube);

db<>fiddle

要将您的数据导入 SQL Server,不要尝试使用诸如 sp_OA 之类的技巧来加载数据。

改为使用类似 Powershell Invoke-WebRequest 的东西,并使用 Invoke-SqlCmd 或 dba-tools 的 Invoke-Query 传递它

Install-Module dbatools
$xml = (Invoke-WebRequest -Uri "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml").Content;

$query = @"THEABOVEQUERY";

Invoke-DbaQuery -SqlInstance "yourserver" -Query $query -SqlParameter @{ Name = "@xml"; Value = $xml; SqlDbType = "Xml" };

【讨论】:

  • 这正是我所问的,所以谢谢,但是我怎样才能直接从 Web 获取这些 XML 值,而不将 XML 放入 Sql Server 中。我希望我很清楚,提前谢谢你,
  • 你可以使用 Powershell 或类似的方法
  • 好的,这就是我解决问题的方法
  • 在此链接中您将找到 suolucion **Solucion **
  • 我说“不要试图使用诸如 sp_OA 之类的黑客”,这是一个真的坏主意。另见dba.stackexchange.com/a/158514/220697stackoverflow.com/a/8328549/14868997
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多