Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need to generate o/p of multiple SQL query from multiple oracle databases. So I want to connect from excel using VB script to multiple database and run all the query at a time.

Please help!!! Here is my script which will connect only to one database and run only one query without heading. How to make it possible to connect multiple database and run multiple query.


CODE :----
VB
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Cmd As New ADODB.Command
Dim sqlText As String
Dim query As String
Dim Row As Long
Dim Findex As Long
Dim Data As Worksheet
Dim X As Long
Dim UID As String
Dim PWD As String
Dim Server As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim strSQL As String
Dim strCon As String
Dim strResult As String
Dim OraDynaSet As Object
Dim i As Integer
Application.Calculation = xlCalculationManual
Set Data = Sheets("Sheet1") 'Change this to the name of the sheet you want to return data to
Data.Select
Range("A:F").ClearContents 'Change A:F to the range you expect data to return to
'---- Replace below highlighted names with the corresponding values
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=<server_name>)(PORT=<port_number>))" & _
"(CONNECT_DATA=(SID=<db_name>))); uid=user_name; pwd=password;"

'---  Open   the above connection string.
con.Open (strCon)
'---  Now connection is open and you can use queries to execute them.
'---  It will be open till you close the connection
Cmd.ActiveConnection = con
Cmd.CommandType = adCmdText

' Put your query next
sqlText = " select name from v$database "

Cmd.CommandText = sqlText
Set rs = Cmd.Execute
Do While Not rs.EOF 'this loops through the data and puts it in the spreadsheet
Row = Row + 1
For Findex = 0 To rs.Fields.Count - 1
Data.Cells(Row + 1, Findex + 1) = rs.Fields(Findex).Value
Next Findex
rs.MoveNext
Loop
Application.Calculation = xlCalculationAutomatic
Application.Calculate
End Sub
Posted
Updated 17-Sep-14 16:42pm
v2

1 solution

Declare multiple connection objects and multiple recordset objects.

E.g.:
VB
Dim con1 As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim Cmd1 As New ADODB.Command
Dim con2 As ADODB.Connection
Dim rs2 As ADODB.Recordset
Dim Cmd2 As New ADODB.Command


For your sanity I suggest you use more meaningful names (e.g., "conCustomers", "cmdOrders")

Use similar code for each set of connections/queries.

Notes:
If there are multiple queries for the same connection, you can reuse the same connection, you' don't need a new one each time.
If the code is very similar for each query you could make the code more generic based on passed arguments and call it multiple times from a parent subroutine (code re-use).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900