問題的提出:如何在form的程序中實(shí)現(xiàn)oracle與access表之間的導(dǎo)入和導(dǎo)出。
問題的解答:
準(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/[email protected]: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/[email protected]: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;
新聞熱點(diǎn)
疑難解答
圖片精選