问题的解答:
准备工作:
1.安装OCA。运行Developer的安装盘,选择自定义安装,选择Oracle Open Client Adapter for ODBC安装。
2.在数据源(ODBC)中添加DSN。控制面板->管理工具->数据源(ODBC),选择“用户DSN”,添加要进行操作的Access的文件。在“高级”选项里,填上“登录名称”和“密码”(很重要,在程序中会用到)。
下面以实际例子来说明:
假设在Oracle中和Access中都有一个student表,表中字段相同(name char(10) ,age number(2)),在准备工作2中的“数据源名”为test,“登录名称”和“密码”都为user。
下面为从Oracle导出到Access的procedure:
以下是引用片段: PROCEDURE oracle_to_access IS connection_id EXEC_SQL.ConnType; action_cursor EXEC_SQL.CursType; ignore PLS_INTEGER; t_name student.name%type; t_age student.age%type; cursor temp_cursor is select * from student; BEGIN connection_id:= EXEC_SQL.OPEN_CONNECTION('user/user@odbc:test'); action_cursor := EXEC_SQL.OPEN_CURSOR(connection_id); EXEC_SQL.PARSE(connection_id, action_cursor,'delete * from student'); ignore := EXEC_SQL.EXECUTE(connection_id, action_cursor); EXEC_SQL.CLOSE_CURSOR(connection_id,action_cursor); open temp_cursor; export_count := 0; action_cursor := EXEC_SQL.OPEN_CURSOR(connection_id); EXEC_SQL.PARSE(connection_id, action_cursor,'INSERT INTO student(name,age) values(:1,:2)'); loop fetch temp_cursor into t_name,t_age; exit when temp_cursor%notfound; EXEC_SQL.BIND_VARIABLE(connection_id,action_cursor, ':1', t_name); EXEC_SQL.BIND_VARIABLE(connection_id,action_cursor, ':2', t_age); ignore := EXEC_SQL.EXECUTE(connection_id, action_cursor); end loop; close temp_cursor; EXEC_SQL.PARSE(connection_id, action_cursor,'commit'); ignore := EXEC_SQL.EXECUTE(connection_id,action_cursor); EXEC_SQL.CLOSE_CURSOR(connection_id,action_cursor); EXEC_SQL.CLOSE_CONNECTION(connection_id); EXCEPTION WHEN EXEC_SQL.PACKAGE_ERROR THEN IF EXEC_SQL.LAST_ERROR_CODE(connection_id) != 0 THEN message('数据导出至ACCESS失败: ' || TO_CHAR(EXEC_SQL.LAST_ERROR_CODE(connection_id)) || ': ' || EXEC_SQL.LAST_ERROR_MESG(connection_id)); END IF; IF EXEC_SQL.IS_CONNECTED(connection_id) THEN IF EXEC_SQL.IS_OPEN(connection_id,action_cursor) THEN EXEC_SQL.CLOSE_CURSOR(connection_id,action_cursor); END IF; EXEC_SQL.CLOSE_CONNECTION(connection_id); END IF; END; 下面为从Access导出到Oracles的procedure: PROCEDURE Access_to_oracle IS connection_id EXEC_SQL.ConnType; action_cursor EXEC_SQL.CursType; ignore PLS_INTEGER; t_name student.name%type; t_age student.age%type; BEGIN connection_id := EXEC_SQL.OPEN_CONNECTION('user/user@odbc:test'); action_cursor := EXEC_SQL.OPEN_CURSOR(connection_id); delete from student; EXEC_SQL.PARSE(connection_id, action_cursor,'select name,age from student'); ignore := EXEC_SQL.EXECUTE(connection_id, action_cursor); exec_sql.define_column(connection_id,action_cursor,1,t_name,10); exec_sql.define_column(connection_id,action_cursor,2,t_age); ignore := EXEC_SQL.EXECUTE(connection_id, action_cursor); while(exec_sql.fetch_rows(connection_id,action_cursor)>0) loop exec_sql.column_value(connection_id,action_cursor,1,t_name); exec_sql.column_value(connection_id,action_cursor,2,t_age); insert into test(name,age) values(t_name,t_age); end loop; commit; EXEC_SQL.CLOSE_CURSOR(connection_id,action_cursor); EXEC_SQL.CLOSE_CONNECTION(connection_id); EXCEPTION WHEN EXEC_SQL.PACKAGE_ERROR THEN IF EXEC_SQL.LAST_ERROR_CODE(connection_id) != 0 THEN message('数据导入至ORACLE失败: ' || TO_CHAR(EXEC_SQL.LAST_ERROR_CODE(connection_id)) || ': ' || EXEC_SQL.LAST_ERROR_MESG(connection_id)); END IF; IF EXEC_SQL.IS_CONNECTED(connection_id) THEN IF EXEC_SQL.IS_OPEN(connection_id,action_cursor) THEN EXEC_SQL.CLOSE_CURSOR(connection_id,action_cursor); END IF; EXEC_SQL.CLOSE_CONNECTION(connection_id); END IF; END; |
注意:EXEC_SQL.BIND_VARIABLE中绑定的变量只能是以下三种类型:NUMBER,DATE,VARCHAR2。对于Access中的“是/否”的布尔型变量,可以用NUMBER类型的1和0来表示。如果Access中的表名或者字段名中有空格,在写SQL语句的时候可以用双引号把表名或者字段名包括起来,如:本例中如果Access中表名为student detail,字段名分别为student name和student age,那插入数据的SQL语句为:insert into “student detail”(“student name”,”student age”) values(:1,:2).