Anda di halaman 1dari 7

Sub GetData()

Dim SQL_String As String


Dim dbConnectStr As String
Set con = New ADODB.Connection
Set recset = New ADODB.Recordset
Dim recordCount As Long
dbConnectStr = "Provider=msdaora;Data Source=" & "Oracle_Database_Name;"

User Id=userId" & "; Password=" & "password"


'The statement above has been commented out. I use the statement below to prompt the user for the userId and password -
which is what I prefer!

con.ConnectionString = dbConnectStr
con.Properties("Prompt") = adPromptAlways
con.Open dbConnectStr 'ConnectionString

'This is an example SQL code that you might want to run


'Select * From MyTable

'SQL_String = "Select count(*) from adm_user"


recset.Open SQL_String, con
recset.MoveLast
recordCount = recset.recordCount
recset.MoveFirst
Do While Not recset.EOF = True
'Have a loop here to go through all the fields
recset.MoveNext
Loop
recset.Close
End Sub

Sub Sample()
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset
Dim strConn As String, strSQL As String
Dim myUser As String, myPassword As String

myUser = "travel"
myPassword = "travel"

strConn = "Provider=MSDAORA;Data Source=192.168.0.195:1521/XE;" & _


"Password=" & myPassword & ";User ID=" & myUser & ";Persist Security Info=True"

conn.Open strConn

strSQL = "select * From airlines"


rs.Open strSQL

rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub

Here you go for simple and easy steps Excel query to Oracle Database using ODBC

http://www.databasejournal.com/features/oracle/article.php/3358411/Connecting-with-Oracle-Accessing-Oracle-via-Access-and-Excel.htm

Regards
http://www.oracleracexpert.com
Overview of Transparent Application Failover in Oracle RAC
http://www.oracleracexpert.com/2010/04/overview-of-transparent-application.html
In function 'lcdprm':: Warning after patch in RAC
http://www.oracleracexpert.com/2010/04/in-function-lcdprm-warning-gets.html
Controlfile and Server parameter file AUTOBackup
http://www.oracleracexpert.com/2010/05/controlfile-and-server-parameter-file.html

Yes, you can use OLE DB...

http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/

Hope this helps,

Regards,
http://www.oracleracexpert.com
Install ASM lib in 10g
http://www.oracleracexpert.com/2009/08/install-and-configure-asmlib-in-10g.html
createdisk, deletedisk and querydisk in ASM
http://www.oracleracexpert.com/2009/09/createdisk-deletedisk-and-querydisk-in.html

MacLochlainns Weblog
Michael McLaughlin's Technical Blog

Query Oracle database


with 108 comments
I caught a post on the OTN forum asking how to do this, and it happened to be something Im working on for a new course that Ill be teaching
on data analytics. Ultimately, Microsoft Excel is the de facto tool of many accounts and financial analysts, protests notwithstanding.

This shows you how to query an Oracle 11g database from Excel 2007 (only available when you do a FULL install of Excel 2007). Actually, it
should work on any current version of the Oracle database. The key to making this work is having the Oracle 10g Client software or an Oracle
11g database on the same machine. The Oracle client software allows you to resolve an Oracle Network Alias (found in the
%ORACLE_HOME%\network\admin\tnsnames.ora file).
Here are the instructions with screen shots:

1. Click on the Data ribbon, and then click on the From Other Sources icon in the Get External Data section, as shown:

2. When you click on the From Other Sources icon, youll get the following drop down list. Click on the From Data Connection Wizard item.

3.The Data Connection Wizard provides a number of Microsoft data sources, but you click the Other/Advancedoption to connect to an Oracle
database.
4. The Other/Advanced selection launches the Data Link Properties dialog with the Provider tab open and active. Click the Oracle Provider for OLE
DB choice. If you dont have the Oracle Provider for OLE DB option, download the Oracle Data Access Components for Windows from Oracle site.
The installation should also load it as a new data source, if not you can follow the same instructions provided for MySQLs ODBC here. After
clicking the Oracle Provider for OLE DB choice, you click the Next button.

5. The Next button brings you to the Connection tab. You should fill it out as noted below, and if you wish check the Allow saving
password checkbox.
6. Before preceding, you should verify that the connection works. Click the Test Connection button to do that, and if successful youll see the
following. Click the OK button in the Microsoft Data Link dialog to complete the test and the OK button in the Data Link Properties dialog.

7. After setting up the data connection in the Data Link Properties dialog, youll be taken to the Data Connection Wizard dialog. Here you can
pick any table that you have access to within the PL/SQL schema (remember a schema in Oracle is a database in Microsoft SQL Server or MySQL).
Click the Next button to continue.
8. After clicking the Next button, youll see the second Data Connection Wizard dialog. Click the Finish button to begin importing the data.

9. The Import Data dialog asks you how and where you want to import it. The following chooses to import it as an ordinary table. Click
the OK button to import the data from a table. If you want to write a query against one or more tables, click here for the additional instructions.
10. Imports into Excel 2007 as a table are automatically created as a table. The following screen shot formats the display columns.

You have a number of very advantageous features in Excel 2007. They allow a number of options that resemble working within a database but
honestly theyre much more difficult than working in SQL. Id advise you create preformatted data by using views. Then, query the views through
these steps.

Written by maclochlainn
November 7th, 2008 at 12:25 am
Posted in Microsoft Excel,Oracle,Oracle 11g,Oracle 12c,Oracle XE,sql

108 Responses to 'Query Oracle database'


Subscribe to comments with RSS or TrackBack to 'Query Oracle database'.
1. Hammad, You can write a SQL Query and assign the results to the Excel page, instead of a table. Ive written the following post to highlight how to use a query
instead of a table reference.

maclochlainn
20 Nov 15 at 11:46 pm
2. Hi,
i want to connect oracle DB using excel 2013(32 bit) in windows8.1 (64 bit)
always throws an error message
Net working components were not found..
Please help me on this

Vinay
26 Nov 15 at 3:23 am

3. Viney, You need 32-bit drivers for Oracle client installed on your server. Otherwise youre trying to connect a 32-bit client software with 64-bit server
software and it wont work.

maclochlainn
21 Dec 15 at 8:25 pm

4. Hi Guys,

We are trying to build a small report, which needs to pull the data from DB into excel file, we are hitting a deadlock, tried most of the options suggested in
google but couldnt figure out a solutionso reaching out to you guys if anyone has tried to integrate excel and Oracle.
1. the specified dsn contains architecture mismatch..
2. oracle client and networking components are not found.

revanth
10 May 16 at 3:50 am

5. Revanth, Ill try to get to it but probably not for a couple weeks.

maclochlainn
10 May 16 at 11:16 pm

6. Weve migrated to a 64 bit version of Excel (Office 2010). We are finding the connections are VERY SLOW. What could cause this? Do we need a 64 bit driver for
the Oracle OLE DB?

Thanks for your help.

Don

Don Wilson
21 Jul 16 at 2:00 pm

7. Don, You should match the 64-bit version of Excel with the 64-bit driver generally.

maclochlainn
16 Oct 16 at 9:01 pm

8. This post was published almost 10 years ago, but as I see the comments are still alive. So Id like to recommend to use some add-in for this task. Theres a lot
of them now and I believe its much more easier way to connect to any DB from Excel. For example Power Query (free) or Devart (its commercial, but it has
great visual query builder)

Anda mungkin juga menyukai