简体   繁体   中英

Excel: refresh SQL from ODBC file

I'm using Excel 2010 to read some SQL data using an ODBC data connection. To do this, I have.odc connection files which I can access through the "Data" tab and the "Existing Connections" button. Excel finds the ODC files, and it can retrieve data from them just fine. Microsoft Query can also edit the queries no problem.

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. You see, this data is simulation output that I'm further processing in Excel; the simulator outputs an SQLite database. 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.

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). 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.

Granted, edits to the SQL query shouldn't happen that often, but they do happen occasionally. 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. As I make mistakes in these calculations, though, I'd like to update them, which means updating the 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. 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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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