問題的解答:
準(zhǔn)備工作:
1.安裝OCA。運(yùn)行Developer的安裝盤,選擇自定義安裝,選擇Oracle Open Client Adapter for ODBC安裝。
2.在數(shù)據(jù)源(ODBC)中添加DSN?刂泼姘->管理工具->數(shù)據(jù)源(ODBC),選擇“用戶DSN”,添加要進(jìn)行操作的Access的文件。在“高級(jí)”選項(xiàng)里,填上“登錄名稱”和“密碼”(很重要,在程序中會(huì)用到)。
下面以實(shí)際例子來說明:
假設(shè)在Oracle中和Access中都有一個(gè)student表,表中字段相同(name char(10) ,age number(2)),在準(zhǔn)備工作2中的“數(shù)據(jù)源名”為test,“登錄名稱”和“密碼”都為user。
下面為從Oracle導(dǎo)出到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('數(shù)據(jù)導(dǎo)出至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導(dǎo)出到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('數(shù)據(jù)導(dǎo)入至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。對(duì)于Access中的“是/否”的布爾型變量,可以用NUMBER類型的1和0來表示。如果Access中的表名或者字段名中有空格,在寫SQL語句的時(shí)候可以用雙引號(hào)把表名或者字段名包括起來,如:本例中如果Access中表名為student detail,字段名分別為student name和student age,那插入數(shù)據(jù)的SQL語句為:insert into “student detail”(“student name”,”student age”) values(:1,:2).