简体   繁体   English

Excel:从 ODBC 文件刷新 SQL

[英]Excel: refresh SQL from ODBC file

I'm using Excel 2010 to read some SQL data using an ODBC data connection.我正在使用 Excel 2010 使用 ODBC 数据连接读取一些 SQL 数据。 To do this, I have.odc connection files which I can access through the "Data" tab and the "Existing Connections" button.为此,我有 .odc 连接文件,我可以通过“数据”选项卡和“现有连接”按钮访问这些文件。 Excel finds the ODC files, and it can retrieve data from them just fine. Excel 找到 ODC 文件,它可以从中检索数据就好了。 Microsoft Query can also edit the queries no problem. Microsoft Query 也可以编辑查询没问题。

The problem, and also the reason I chose.odc-files, is that I'd like to be able to change the underlying data source of the queries and also the queries themselves.问题,也是我选择 .odc-files 的原因,是我希望能够更改查询的基础数据源以及查询本身。 You see, this data is simulation output that I'm further processing in Excel;你看,这个数据是模拟 output,我在 Excel 中进一步处理; the simulator outputs an SQLite database.模拟器输出一个 SQLite 数据库。 I regularly make changes in the simulator to fix bugs, then do a rerun to obtain new data.我定期在模拟器中进行更改以修复错误,然后重新运行以获取新数据。 The output format (ie, table structure and format and all that) is the same, it's just the data that's different. output 格式(即表结构和格式等)是相同的,只是数据不同。

Hence, the idea is to be able to just edit the.odc-file to use a database with newer simulation results, hit refresh in Excel, and be done with it (the.odc files contain both the connection string and the SQL string).因此,我们的想法是能够只编辑 .odc 文件以使用具有较新模拟结果的数据库,在 Excel 中刷新,然后完成它(.odc 文件包含连接字符串和 SQL 字符串) . After working throughthe horribly bugged "always use this connection" checkbox , I've found that Excel will pick up on any edits to the connection string (ie, changing the database file), but it refuses to pick up on any changes to the SQL.通过可怕的错误“始终使用此连接”复选框后,我发现 Excel 将接受对连接字符串的任何编辑(即更改数据库文件),但它拒绝接受对连接字符串的任何更改SQL。

Granted, edits to the SQL query shouldn't happen that often, but they do happen occasionally.当然,对 SQL 查询的编辑不应该经常发生,但它们确实偶尔会发生。 It's not uncommon for me to do some calculations in SQL instead of afterwards in Excel, because frankly mixing "live" DataTable columns (or whatever it's called) and "regular" derived columns sounds like a world of hurt.在 SQL 中而不是之后在 Excel 中进行一些计算对我来说并不少见,因为坦率地混合“实时”数据表列(或其他任何名称)和“常规”派生列听起来像是一个受伤的世界。 As I make mistakes in these calculations, though, I'd like to update them, which means updating the SQL.但是,当我在这些计算中出错时,我想更新它们,这意味着更新 SQL。

So apparently Excel will read the connection string from the ODC file, but will happily ignore the SQL query in it and (I presume) store it in the XLS file instead.所以显然 Excel将从ODC 文件中读取连接字符串,但会很高兴地忽略其中的 SQL 查询,并且(我认为)将其存储在 XLS 文件中。 I can only imagine the horror if you're in a company setting and have some analysis queries or something stored in ODC files, only to discover that you get to make the changes manually on every single computer company-wide.我只能想象如果您在公司环境中并且有一些分析查询或存储在 ODC 文件中的内容,却发现您可以在公司范围内的每台计算机上手动进行更改,那会是多么可怕。

Am I doing something wrong, or is this the intended behaviour?我做错了什么,还是这是预期的行为? Is there a way to get Excel to actually use the SQL from the ODC files instead of always using its local version?有没有办法让 Excel 实际使用 ODC 文件中的 SQL 而不是始终使用其本地版本?

A few things:一些东西:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM