【问题标题】:Excel: refresh SQL from ODBC fileExcel:从 ODBC 文件刷新 SQL
【发布时间】:2011-07-16 19:25:17
【问题描述】:

我正在使用 Excel 2010 通过 ODBC 数据连接读取一些 SQL 数据。为此,我有 .odc 连接文件,我可以通过“数据”选项卡和“现有连接”按钮访问这些文件。 Excel 会找到 ODC 文件,并且可以很好地从中检索数据。 Microsoft Query 也可以编辑查询没问题。

问题,也是我选择 .odc 文件的原因,是我希望能够更改查询的基础数据源以及查询本身。你看,这些数据是我在 Excel 中进一步处理的模拟输出;模拟器输出一个 SQLite 数据库。我定期在模拟器中进行更改以修复错误,然后重新运行以获取新数据。输出格式(即表结构和格式等)是相同的,只是数据不同。

因此,我们的想法是能够只编辑 .odc 文件以使用具有较新模拟结果的数据库,在 Excel 中点击刷新,然后完成(.odc 文件包含连接字符串和SQL 字符串)。通过the horribly bugged "always use this connection" checkbox 工作后,我发现Excel 会接收对连接字符串的任何编辑(即更改数据库文件),但它拒绝接收对SQL 的任何更改。

诚然,对 SQL 查询的编辑不应该经常发生,但它们确实偶尔会发生。在 SQL 中而不是之后在 Excel 中进行一些计算对我来说并不少见,因为坦率地混合“实时”DataTable 列(或其他任何名称)和“常规”派生列听起来像是一个痛苦的世界。不过,由于我在这些计算中犯了错误,我想更新它们,这意味着更新 SQL。

显然,Excel 从 ODC 文件中读取连接字符串,但很乐意忽略其中的 SQL 查询,并且(我认为)将其存储在 XLS 文件中。我只能想象如果您在公司环境中并且有一些分析查询或存储在 ODC 文件中的内容,却发现您可以在公司范围内的每台计算机上手动进行更改,那会是多么可怕。

我做错了什么,还是这是预期的行为?有没有办法让 Excel 实际使用 ODC 文件中的 SQL,而不是始终使用其本地版本?

【问题讨论】:

    标签: sql excel odbc


    【解决方案1】:

    一些事情:

    Part 1
     - Create a template .ODC file.  
        - Create a connection Data tab, From Other Sources, follow the wizard, connect to the DB and data table.
     - Go to the Data tab, Connections and set the connection properties that you want.
     - Export the connection
    
    Part 2
     - Create the working SQL query for SQLite that produces the desired results
     - Put the entire SQL query on one line (odc files like everything on one line, don't ask)
    
    Part 3
     - Right click on the .odc file, use 'Open with' and open it in notepad or notepad++ (NOT WORD)
     - Look for <odc:CommandText> </odc:CommandText>
     - Remove the text that is there and paste your one line query between the commandtext tags
     - Save the .odc file
     - Double click on the .odc file and see if the data opens/appears in Excel
    
    Don't forget to double check to make sure the data in the <odc:ConnectionString> </odc:ConnectionString> is correct.
    

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多