Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: HenkaPosted on 2004-04-29 at 02:36:29ID: 10947375
Here is Note:115540.1 from MetaLink: How to Copy Data from ODBC Datasource to Oracle using Forms
source_db) ; dest_db); e_connid); id, source_cursor,'SELECT empno, ename,deptno FROM ' ||:source_tab); EXEC_SQL.DEFINE_COLUMN(sou rce_connid , source_cursor,1,empno); rce_connid , source_cursor,2,ename,10); rce_connid , source_cursor,3,deptno); nnid,sourc e_cursor); nation_con nid); _connid,de stination_ cursor,'IN SERT INTO ' ||:dest_tab ||'(empno,ename,deptno) VALUES (:empno,:ename,:deptno)'); _connid,so urce_curso r) > 0 THEN ce_connid, source_cur sor,1, empno); ce_connid, source_cur sor,2, ename); ce_connid, source_cur sor,3, deptno); tination_c onnid,dest ination_cu rsor, ':empno', empno); tination_c onnid,dest ination_cu rsor, ':ename', ename); tination_c onnid,dest ination_cu rsor, ':deptno', deptno); on_connid, destinatio n_cursor); _connid,de stination_ cursor,'co mmit'); on_connid, destinatio n_cursor); ination_co nnid,desti nation_cur sor); ce_connid, source_cursor); destinatio n_connid); source_con nid);
ource_conn id) != 0 THEN R_CODE(sou rce_connid ))|| '): ' ||EXEC_SQL.LAST_ERROR_MESG (source_co nnid));
estination _connid) != 0 THEN R_CODE(des tination_c onnid))|| '): ' ||EXEC_SQL.LAST_ERROR_MESG (destinati on_connid) );
ination_co nnid) THEN on_connid, destinatio n_cursor) THEN ination_co nnid,desti nation_cur sor); destinatio n_connid);
ce_connid) THEN nnid,sourc e_cursor) THEN ce_connid, source_cur sor); source_con nid);
asource)
This article describes how to copy data from an ODBC datasource such as Access into a table in an Oracle database using Oracle Forms. The method uses the client-side EXEC_SQL package to open multiple connections and copy the data.
The article is intended for Forms programmers who may be new to ODBC.
The code provided is only for demonstration of a general principle - in practice additional code appropriate to the application would be required to improve error-handling and robustness.
INTRODUCTION
It is sometimes necessary to be able to copy data between non-Oracle datasources and Oracle databases (and vice versa) without dumping the data out to a flat file and using tools such as SQLLoader to reload the data.
The use of the EXEC_SQL package from a client tool (eg Forms, Reports, Procedure Builder) with the Open Client Adapter (OCA) for ODBC allows multiple connections to both Oracle and non-Oracle databases to be opened simultaneously.
Developer 6.0.x has the EXEC_SQL package built in. In earlier releases of Developer the functionality was provided by a PL/SQL library (.pll file).
This note assumes you are using Developer 6.0.x
EXEC_SQL is similar in many respects to the server-side DBMS_SQL package, however see the Procedure Builder online help for further information on the differences between these packages.
BACKGROUND
You have a table called emp in an Access 97 database which has columns empno, ename and deptno and you wish to copy the data from this table into a table in your Oracle database. For simplicity we will assume that the table in the Oracle database is also called emp which also has columns empno, ename and deptno.
The sample code provided shows how to do this, however it makes no attempt to check for the failure of the insert into the Oracle table (eg if primary/ foreign key constraints are violated). Additional code would be required to perform these checks.
SOLUTION
1. Add the ODBC datasource for your Access database.
In the Control Panel choose the ODBC Data Sources icon and click on the Add button.
Choose the MS Access driver and click on the Finish button.
In the window that now appears type in the name of the datasource (eg empaccess), a description and choose the Access .mdb file of your database by clicking on the Select button.
2. Build the Form.
Before doing this make sure you have installed Oracle Open Client Adapter for ODBC - this is available from the Developer CD.
Create a control block with four text items of datatype Char.
The items are called:
source_db - which holds the connect string for the Access database
source_tab - the name of the Access table to be copied
dest_db - the connect string for the Oracle database
dest_tab - the name of the Oracle table into which data will be copied
Set the Required property of all four text items to Yes
Create a button item called copy in this control block.
Place the following code in an ON-LOGON trigger at Form level: NULL;
Place the following code in a WHEN-BUTTON-PRESSED trigger on the copy button:
declare
empno NUMBER;
ename VARCHAR2(10);
deptno NUMBER;
source_connid EXEC_SQL.ConnType;
destination_connid EXEC_SQL.ConnType;
source_cursor EXEC_SQL.CursType;
destination_cursor EXEC_SQL.CursType;
dummy PLS_INTEGER;
begin
-- open the connections
source_connid := EXEC_SQL.OPEN_CONNECTION(:
destination_connid := EXEC_SQL.OPEN_CONNECTION(:
-- prepare the cursor to select from source database
source_cursor := EXEC_SQL.OPEN_CURSOR(sourc
EXEC_SQL.PARSE(source_conn
EXEC_SQL.DEFINE_COLUMN(sou
EXEC_SQL.DEFINE_COLUMN(sou
dummy := EXEC_SQL.EXECUTE(source_co
-- prepare the cursor to insert into destination database
destination_cursor := EXEC_SQL.OPEN_CURSOR(desti
EXEC_SQL.PARSE(destination
LOOP
-- fetch rows from source database
IF EXEC_SQL.FETCH_ROWS(source
-- get column values for this row
EXEC_SQL.COLUMN_VALUE(sour
EXEC_SQL.COLUMN_VALUE(sour
EXEC_SQL.COLUMN_VALUE(sour
-- bind the column values into the cursor that inserts into destination database
EXEC_SQL.BIND_VARIABLE(des
EXEC_SQL.BIND_VARIABLE(des
EXEC_SQL.BIND_VARIABLE(des
dummy := EXEC_SQL.EXECUTE(destinati
ELSE
EXIT;
END IF;
END LOOP;
-- commit transaction in destination database
EXEC_SQL.PARSE(destination
dummy := EXEC_SQL.EXECUTE(destinati
-- close cursors and connections
EXEC_SQL.CLOSE_CURSOR(dest
EXEC_SQL.CLOSE_CURSOR(sour
EXEC_SQL.CLOSE_CONNECTION(
EXEC_SQL.CLOSE_CONNECTION(
MESSAGE('Done!');
EXCEPTION
-- handle errors raised by the exec_sql package
WHEN EXEC_SQL.PACKAGE_ERROR THEN
IF EXEC_SQL.LAST_ERROR_CODE(s
TEXT_IO.PUT_LINE('ERROR (source: ' ||
TO_CHAR(EXEC_SQL.LAST_ERRO
END IF;
IF EXEC_SQL.LAST_ERROR_CODE(d
TEXT_IO.PUT_LINE('ERROR (destination: ' ||
TO_CHAR(EXEC_SQL.LAST_ERRO
END IF;
IF EXEC_SQL.IS_CONNECTED(dest
IF EXEC_SQL.IS_OPEN(destinati
EXEC_SQL.CLOSE_CURSOR(dest
END IF;
EXEC_SQL.CLOSE_CONNECTION(
END IF;
IF EXEC_SQL.IS_CONNECTED(sour
IF EXEC_SQL.IS_OPEN(source_co
EXEC_SQL.CLOSE_CURSOR(sour
END IF;
EXEC_SQL.CLOSE_CONNECTION(
END IF;
END;
Compile, save and generate the form.
3. Run the Form
In the Source_Db item enter the connection string for the Access database eg /@odbc:empaccess (assuming there is no username/password on the Access database and empaccess is the name of the datasource defined in the Control Panel - in general the format of the connection string is
username/password@odbc:dat
In the Source_Tab item enter the name of the Access table to be copied eg emp
In the Dest_Db item enter the connection string for the Oracle database to which the data is to be copied eg scott/tiger@mydb where mydb is defined as a valid connection in your tnsnames.ora
In the Dest_tab item enter the name of the table to which the data will be copied eg emp
Now press the copy button.
The data should now be copied from the emp table in the Access database to the emp table in the Oracle database.
RELATED DOCUMENTS - Online Help for Procedure Builder
I hope this will help you.