SQL behind an external data query in Excel - sql

I have inherited an Excel spreadsheet which contains an external data query. I can edit the query, which provides a list of the columns returned, but it does provide the table that this data comes from. Is there any way to retrieve the actual SQL behind the query?

I think this may depend on the version of office you're using and the type of external data you're connecting to.
I believe if you're using Office 2003, and you're seeing columns like you say, there may be a SQL icon in the toolbar.
Click that to view the SQL used to return the data.
If you're using Office 2007, Go to the Data tab, click connections, then properties, then the definition tab, and change the command text.
However I believe you're using the first method.
This is from what I can remember, so I hope it's correct.

It could also be somewhat obsfuscated by using an ODBC link or something. Another way to get it is to run SQL Profiler on the SQL Server to see the exact SQL being executed. Of course, this won't help you edit it but you'll know what it is doing.

Related

Excel OBDC - SQL Query

I am trying to use Excel pull data from a large Oracle Data Warehouse via an ODBC connection. I have a query that works using the editor in Access. I've tried using Power Query and Microsoft SQL to use this query to get this data into Excel and I get errors.
Therefore:
Does SQL executed from Excel need to be in a different syntax? Shouldn't it still be Oracle?
How can I use this pre-written query to ping the data warehouse and get what I need?
Here is the SQL that I have so far. I had to change some table names...sorry if that makes it weird.
The SQL you posted uses Access-specific functions. That is NOT a valid SQL query if run directly against Oracle. If you have a bunch of linked tables in Access, that would allow this.
The ODBC datasource connection in Excel works differently. ODBC executes the query directly at the datasource. It does some validation first and supports a limited subset of SQL language supported at the destination. What's included in the subset is determined in part by the driver selected for the connection.
So what you want to do is use a tool that lets you build the query directly in an Oracle environment, like Quest Toad or Oracle SQL Developer. Once you have the query working there, it should be easier to port it to Excel.
One thing I like to do is put my query into a view on the database. Then I can just select everything from the view when creating the Excel connection.
It's also worth pointing you to the My Data Sources folder. When you first setup an ODBC connection in Excel, the connection is saved by default in Windows in a folder called "My Data Sources" located just under your user profile folder. For example: C:\Users\UserName\My Data Sources\Data Source Name.odc.
You can open these *.odc files in any text editor, and you should be able to manually edit the SQL here. Especially look for the <odc:CommandText> element. In this way you can build a simple query up front, and then improve on the SQL command in your favorite environment and easily move the updated SQL to the existing ODBC connection.

Export SQL code from Excel into SQL

My skill level: basic user of VBA, know next to nothing about SQL (though I’ve recently learned how to execute code, etc).
I’ve created a user interface in an Excel worksheet to allow user input of report variables (dates, etc). These pass into a second Excel worksheet that, through the use of various Excel statements, creates the necessary SQL code. My macro then copies this worksheet and opens Microsoft SQL Server 2008 Rx via a shell command. Currently, I then manually click “connect” on the resultant Microsoft SQL Server 2008 R2 connect-to-server window (which does not need a password, just requires me to click “connect”), I create a new query, I paste the code in and then execute in SQL. So it is these latter steps (opening SQL, click “connect”, copy/paste code, execute) that I’m trying to automate via VBA.
To offer more context, once the SQL code has run, I then have a second macro that uses the Excel Data Connection wizard to bring the resultant SQL data back into my worksheet. This is working well.
I’ve researched this for several days on various boards. None seem to observe my approach of creating the code in Excel (probably because it is an inelegant approach). Or the post responses are beyond my skill level to understand. But I do fear that I’m asking a question that has been answered before (apologies in advance).
Thanks for whatever advice/time can be offered.
I’m using:
Excel/Office 2010
Microsoft SQL Server2008 R2 (is this the same as Microsoft SQL Server Management Studios? My SQL application shows both names)
Microsoft ActiveX Data Objects 6.1 Library (this is NOT currently checked off. One post I saw referenced the need for this. I have multiple prior AciveX Library versions available as well.)
What you're doing is very ingenious, but it's probably not necessary. Excel has a number of features for importing external data.
I would look at the Data tab in Excel and the From Other Sources section of the ribbon. You have at least three options: the From SQL Server, From Data Connection Wizard, and From Microsoft Query options.
All of these will set up a connection to a database server and execute SQL against it. They return data to your spreadsheet in different ways.
Where VBA will come into play again is in automating the generation and parameterization of the queries these will execute.
But I would set up something that works, with constants for your parameter values, first, so you get an idea of the possibilities, and then read and research dynamically setting your SQL with VBA.
You might also investigate PowerPivot, by the way.

Can Powerpivot be used to present data without pivoting it?

This isn't a coding question, so much as it is a functionality question.
I'm a bit new to Powerpivot and I'm attempting to use Powerpivot as a way to deliver reports to my team rather than SSRS because in our company, the only SSRS ReportServer I can use is officially "owned" by a team that is hostile to my team.
I have written numerous SQL ad-hoc queries that we are using for reports in SSRS, but been I've trying to migrate them to Powerpivot. For some of the reports, I've been able to paste the SQL query into the powerpivot window and make a pivottable out of these. However, I'd like to be able have some workbooks just contain the results of the query (they don't deal with numbers, as some of them are a data dump or a large collection of text strings), so that they look like the powerpivot window's dataset, but that they can be filtered, sorted, etc. Is this possible, or can I only use pivottables? I can't seem to find a way that I can just get Excel to directly display what is in the Powerpivot window without pivoting it - I know it is called Power*pivot* but because the PP window just shows the query results itself, I'm hoping there is a way to use it as an alternative to SSRS.
Also is there anyway to check if our Sharepoint server is capable of running Powerpivot so that I can deploy reports there for users to use without having Powerpivot installed on their computers other than by saving it to Sharepoint and seeing if a user can run it? If not, how difficult would it be to have an IT team update the SP server so that it can handle PP?
Any help is appreciated, as I'm in a position where I cannot contact IT or project management, and I can't raise any red flags with IT/BI/PM. Thank you very much for your time.
It sounds like you just want to get the results of your SQL query directly into an Excel spreadsheet, where you can use the Excel filter and sort capability. It doesn't appear that you want to use any of the functionality that PowerPivot provides, so you can achieve what you described by using an existing Excel component, Microsoft Query. It's a bit of a clunky old interface but it works. Here's some instructions to use:
In an Excel spreadsheet, click on the Data tab, select the "From Other Sources" button on the Get External Data area of the ribbon.
Select "From Microsoft Query"
On the "Choose Data Source" click OK to create a new data source
On the "Create new data source" window select the appropriate driver and database details for your data source
If using a SQL Server data source, don't select anything for the optional step 4 (select a default table).
Click OK to return to the Choose Data Source window, then OK again to select the new data source.
You're now prompted to choose columns for your query, but if you already have some SQL then click Cancel - this will display a prompt to continue editing in Microsoft Query - click Yes.
The Add Tables window pops up, click Close
You're now in the Microsoft Query interface. If you click on the SQL button, you can paste in your SQL. Click the Return Data button to run the query, the results will be displayed in an Excel worksheet.
Once this is set up, you just need to click "Refresh Data" on the Data tab to re-run the query and update the spreadsheet.

How to view and change data of a .mdf file using MS Access?

Background: I have an .mdf database containing some numeric values that I need to change (specifically, scale and offset values for sensors). The database was created by a Visual Basic program that controls some lab equipment and stores pertinent data in the .mdf file. Please excuse my rudimentary knowledge of the topic; I'm a complete novice when it comes to SQL Server and VB.
After searching through many posts, I understand that to open the .mdf file, you must "attach" it in Microsoft Server Management Studio (I'm using that with SQL Server 2005), which I have successfully done.
Problem: It seems that this only allows me to view and modify the structure of the database, ie. tables, columns, data types etc., rather than the values of the database itself.
Is there a way to do this manually, like how you can edit the field data of .mdb files in MS Access? Again, sorry for my limited knowledge on the topic. I'll gladly provide extra details where needed.
You mention you want to use access. That can be done. Your database is up and running after attaching it.
In Access you can simply create an ODBC connection in access and use Access to edit/modify your data as much as you want. you can also create queries.
It is in the menu item "external data" [I'm guessing because I use a dutch version]. You select to create a "linked table", choose your server and you are done.
Right-click on the table you want to modify in SSMS and select "Edit Top 200 Rows".
If you have more than 200 rows to edit, you can connect to the SQL Server database with either Access or Excel.
Here are the instructions for Access:
http://office.microsoft.com/en-us/access-help/connect-an-access-project-to-a-microsoft-sql-server-database-adp-HP005274586.aspx

extract raw SQL query from a Crystal Report .rpt file

I've got an .rpt file that I did not write and can find no documentation about. I want to be able to review the SQL that is generated from this report so that I can figure out, well, what data it was pulling and what WHERE clause parameters were used.
I can open it up and see the report layout. But when I select Database|Show SQL Query... the report tries to connect to the data source. The problem is, the data source being used is unknown to me, probably an ODBC connection used by whoever wrote the query. All I can do at that stage is 'Cancel' and I'm back to looking at the report designer.
Am I missing something? Can I get to the SQL query without connecting to the datasource? It seems like viewing the selection criteria shouldn't be dependent on a data connection.
Thanks.
version: Crystal Reports 2008
I know that this is an old thread, but I encountered this same problem. Effectively we used to have a database/application that has since been aquired by an external agency.
Although they now have the database/application they don't have access to crystal reports, so we can't just send them the old report that we used to run. Likewise we can't run it as we don't even have the database set up anywhere.... So instead our plan was just to extract the SQL code generated by the report and forward that on.
We experienced the same problem, but the solution is actually pretty simple.
If you don't have access to the original data source, just create a new 'blank' datasource (such as an ODBC connection). As long as the connection to the datasource works (i.e. it is some kind of valid datasource this it works fine). When running the 'Show SQL' option point the report to this datasource. As long as you don't try to actually run the report (and only show the SQL) the operation wont fail. This worked for our situation anyway. (Crystal Reports 2008)
(I can give more details if it helps in any way.)
It should be possible to find out some details about the existing datasource, by selecting Database > Set Datasource Location... .
As well as enabling you to change the datasource location, this should show you some information about the current datasource, such as which type of datasource is being used, and possibly (dependant on the type of driver) the name of the database. It is likely to be less helpful if (as you surmise) the datasource is ODBC, but if it uses a native driver there may be something useful.
Without the password, I'm not sure how much you can do. It seems "Show SQL Query" requires to report to run first, then generate the SQL plan.
It's not ideal, but you could go to Database > Visual Linking Expert to at least see the tables and how they are joined, and the go to the Record Selection Formula Editor and see what the custom WHERE statements are.
Viewing the SQL of a Command in a Crystal Report File
There are times you have just the report file, but not the associated database structure that the report uses.
This is common when dealing with example reports of functionality you wish to mimic.
This is a workaround ONLY to allow you to see the SQL of a Command that a Crystal Report is based on, when you don't have the underlying database connection that the report is based on.
In essence, the dialog box has to be satisfied before it will show the SQL, so we fool it with a legitimate Data Source, just not one that would work with the SQL that is actually in the SQL Command.
Why does a report use a command? Doesn't Crystal Reports have the ability to link tables?
When a Crystal Report is based on a record set that is too complex for the table linking functionality within Crystal Reports, the report can instead be based on a SQL Query, usually developed/tested in another editor tool and pasted into the command. This allows advanced SQL functions to be utilized.
If you don't already have a Data Source on your computer set up that you can connect to, you will need to build one first.
A simple Microsoft Access .mdb file saved in a simple location will suffice.
I placed mine with the path C:\A_test\test.mdb to make it easy to find.
If you don't have one, google for a sample mdb file and download it, saving it with a name and location you can remember. (You won't ever actually open this file, but just connect to it.)
Once you have the file saved, open the ODBC Administrator and create a New Data Source.
(you can get to the ODBC Administrator quickly from Start > type ODBC in the Search)
On the User DSN tab, click the Add button.
Scroll down the driver list to Microsoft Access Driver (*.mdb), select it and click the Finish button.
In the Data Source Name box, type a name (I used MyTest).
Click the Select Button and select the mdb file you saved from a previous step, click OK.
Click OK again. You will see your new Data Source listed by the name you gave it. Click OK.
You now have the data source you will need for the next steps.
Open the Crystal Report you want to see the SQL command for, and click on Database Expert button or Database>Database Expert Menu.
Under Selected Tables, right click on the Command and choose View Command
The Data Source Selection Box appears. Select the Data Source you created (or one you already use) and click the Finish button. The View Command box should open with the SQL in the left pane. Copy the SQL into your favorite text editor.
Whats happening is that the crystal reports needs a database to connect to regardless if its the original source DB or not.
Create a local database or use a database stored on a server, added it to your ODBC Datasources and use it when connecting. After a successful connection you should be able to view the SQL query without an error.