Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odbc & Sql connection in one query

I wanna select some records from the informix database via Odbc connection and insert them to a Sql database table.

INSERT INTO SAS.dbo.disconnectiontemp 
        (meterno) 
SELECT DISTINCT met_number                 
FROM   Bills.dbadmin.MeterData 

I've searched regrading that, but they didn't solve my issue. Is it possible to have both connections at one place?
Any help or suggestions would be appreciated. Thanks

like image 558
Kayathiri Avatar asked Jun 15 '16 06:06

Kayathiri


People also ask

What is ODBC used for?

An ODBC driver uses the Open Database Connectivity (ODBC) interface by Microsoft that allows applications to access data in database management systems (DBMS) using SQL as a standard for accessing the data. ODBC permits maximum interoperability, which means a single application can access different DBMS.

What is ODBC vs SQL?

ODBC provides data types and functions that help applications to interact with the database. SQL is used to create queries to manipulate the data stored in a database.

What is ODBC and how it works?

ODBC is a call-level interface that allows applications to access data in any database for which there is an ODBC driver. Using ODBC, you can create database applications with access to any database for which your end user has an ODBC driver.

Is ODBC still a thing?

ODBC remains in wide use today, with drivers available for most platforms and most databases.


1 Answers

I believe that an ODBC connection is made using an ODBC driver custom-made for the specific DB engine (e.g. Oracle, MSSQL, PSQL, etc.), and hence, a sole query cannot contain two different database engines as the query runs through a specific driver through the ODBC interface.

However, you can easily utilize two ODBC drivers in on code using a simple script in any programming language which has an ODBC library. For example, I use Python along with pyodbc to initialize multiple connections and transfer data between MSSQL, MySQL and PSQL databases. Here's a pseudo-code example:

import pyodbc
psql_cursor = pyodbc.connect('<PSQL_ODBC_CONNECTION_STRING>').cursor()
mysql_cursor = pyodbc.connect('<MYSQL_ODBC_CONNECTION_STRING>').cursor()
result_set = mysql_cursor.execute('<SOME_QUERY>').fetchall()
to_insert = <.... Some code to transform the returned data if needed ....>
psql_cursor = psql_cursor.execute('insert into <some_table> VALUES (%s)' % to_insert)

I realize I'm taking you in a different direction, but hopefully this is still helpful in someway. Happy to provide other examples if needed.

like image 58
Yuval Avatar answered Sep 22 '22 08:09

Yuval