以下是引用片段:
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;
|