一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

用Forall與bulk collect快速?gòu)?fù)制表數(shù)據(jù)-入門(mén)基礎(chǔ)

 krrish 2010-05-18
本文中介紹的幾種寫(xiě)法分別是從代碼的簡(jiǎn)易性,FORALL和bulk collect的使用,以及分批插入這三方面考慮得出的,大家可以根據(jù)自己的需要靈活選擇。
三種不同的寫(xiě)法:
1.使用了BULK COLLECT,沒(méi)有使用FORALL, 一次性插入,分批COMMIT,這種方法比較適用于10萬(wàn)以下條數(shù)據(jù)的表;
create or replace procedure cp_data2 as
type TYPE_EMPLOYEES is table of EMPLOYEES%rowtype;
V_EMPLOYEES TYPE_EMPLOYEES;
v_table varchar2(30);
v_sql varchar2(300);
 v_rows number:=5000;
begin
execute immediate 'alter session set nls_date_format=''yyyy/mm/dd''';
v_table := 'employee_cp';
v_sql := 'insert /*+ APPEND*/ into ' || v_table ||
' (EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID,
SALARY,
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID,
BIRTHDAY)
values (:1, :2,:3,:4,:5,:6, :7, :8,:9,:10, :11,:12)';

select * bulk collect into V_EMPLOYEES from employees; --dest table
for i in 1 .. V_EMPLOYEES.count loop
execute immediate v_sql
using V_EMPLOYEES(i).EMPLOYEE_ID, V_EMPLOYEES(i).FIRST_NAME, V_EMPLOYEES(i).LAST_NAME, V_EMPLOYEES(i).EMAIL, V_EMPLOYEES(i).PHONE_NUMBER, V_EMPLOYEES(i).HIRE_DATE, V_EMPLOYEES(i).JOB_ID, V_EMPLOYEES(i).SALARY, V_EMPLOYEES(i).COMMISSION_PCT, V_EMPLOYEES(i).MANAGER_ID, V_EMPLOYEES(i).DEPARTMENT_ID, V_EMPLOYEES(i).BIRTHDAY;
if mod(i, v_rows) = 0 then
commit;
end if;
end loop;
commit;
end;
 
 
2.使用BULK COLLECT,不使用FORALL, 分批插入,多次提交,比較適用于大表;
create or replace procedure cp_data5 as
type t_cur is REF cursor;
c_table t_cur;
type t_employee is table of employees%rowtype;
v_employees t_employee;
rows number := 50;
v_sql varchar2(300);
v_table varchar(50);
begin
v_table := 'employee_cp';
open c_table for
select * from employees; --sour
v_sql := 'insert /*+ APPEND*/ into ' || v_table ||
' (EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID,
SALARY,
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID,
BIRTHDAY) values (:1, :2,:3,:4,:5,:6, :7, :8,:9,:10, :11,:12)';
loop
fetch c_table bulk collect
into v_employees limit rows; --分批
dbms_output.put_line(v_employees.count);
for i in 1 .. v_employees.count loop
execute immediate v_sql
using V_EMPLOYEES(i).EMPLOYEE_ID, V_EMPLOYEES(i).FIRST_NAME, V_EMPLOYEES(i).LAST_NAME, V_EMPLOYEES(i).EMAIL, V_EMPLOYEES(i).PHONE_NUMBER, V_EMPLOYEES(i).HIRE_DATE, V_EMPLOYEES(i).JOB_ID, V_EMPLOYEES(i).SALARY, V_EMPLOYEES(i).COMMISSION_PCT, V_EMPLOYEES(i).MANAGER_ID, V_EMPLOYEES(i).DEPARTMENT_ID, V_EMPLOYEES(i).BIRTHDAY;
end loop;
commit;
exit when c_table%notfound;
end loop;
close c_table;
end;
 
3.使用BULK COLLECT和FORALL ,分批插入,多次提交,比較適用于大表; 前期數(shù)據(jù)字段定義比較煩鎖(表各個(gè)字段必須分開(kāi)定義)
-------------------
create or replace procedure cp_data as
type type_EMPLOYEE_ID is table of EMPLOYEES.EMPLOYEE_ID%type;
type type_FIRST_NAME is table of EMPLOYEES.FIRST_NAME%type;
type type_LAST_NAME is table of EMPLOYEES.LAST_NAME%type;
type type_EMAIL is table of EMPLOYEES.EMAIL%type;
type type_PHONE_NUMBER is table of EMPLOYEES.PHONE_NUMBER%type;
type type_HIRE_DATE is table of EMPLOYEES.HIRE_DATE%type;
type type_JOB_ID is table of EMPLOYEES.JOB_ID%type;
type type_SALARY is table of EMPLOYEES.SALARY%type;
type type_COMMISSION_PCT is table of EMPLOYEES.COMMISSION_PCT%type;
type type_MANAGER_ID is table of EMPLOYEES.MANAGER_ID%type;
type type_DEPARTMENT_ID is table of EMPLOYEES.DEPARTMENT_ID%type;
type type_BIRTHDAY is table of EMPLOYEES.BIRTHDAY%type;
V_EMPLOYEE_ID TYPE_EMPLOYEE_ID;
V_FIRST_NAME TYPE_FIRST_NAME;
V_LAST_NAME TYPE_LAST_NAME;
V_EMAIL TYPE_EMAIL;
V_PHONE_NUMBER TYPE_PHONE_NUMBER;
V_HIRE_DATE TYPE_HIRE_DATE;
V_JOB_ID TYPE_JOB_ID;
V_SALARY TYPE_SALARY;
V_COMMISSION_PCT TYPE_COMMISSION_PCT;
V_MANAGER_ID TYPE_MANAGER_ID;
V_DEPARTMENT_ID TYPE_DEPARTMENT_ID;
V_BIRTHDAY TYPE_BIRTHDAY;
type t_cur is ref cursor;
c_table t_cur;
v_table varchar2(30); --dest table
v_sql varchar2(300);
v_rows number := 50;
begin
v_table := 'EMPLOYEE_CP';
open c_table for
select * from employees; --sour table
v_sql := 'insert /*+ APPEND*/ into ' || v_table ||
' (EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID,
SALARY,
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID,
BIRTHDAY)
values (:1, :2,:3,:4,:5,:6, :7, :8,:9,:10, :11,:12)';
loop
fetch c_table --.EMPLOYEE_ID, c_table.FIRST_NAME, c_table.LAST_NAME, c_table.EMAIL, c_table.PHONE_NUMBER, c_table.HIRE_DATE, c_table.JOB_ID, c_table.SALARY, c_table.COMMISSION_PCT, c_table.MANAGER_ID, c_table.DEPARTMENT_ID, c_table.BIRTHDAY
bulk collect
into V_EMPLOYEE_ID, V_FIRST_NAME, V_LAST_NAME, V_EMAIL, V_PHONE_NUMBER, V_HIRE_DATE, V_JOB_ID, V_SALARY, V_COMMISSION_PCT, V_MANAGER_ID, V_DEPARTMENT_ID, V_BIRTHDAY limit v_rows; --分批
forall i in 1 .. V_EMPLOYEE_ID.count execute immediate v_sql using
V_EMPLOYEE_ID(i), V_FIRST_NAME(i), V_LAST_NAME(i),
V_EMAIL(i), V_PHONE_NUMBER(i), V_HIRE_DATE(i),
V_JOB_ID(i), V_SALARY(i), V_COMMISSION_PCT(i),
V_MANAGER_ID(i), V_DEPARTMENT_ID(i), V_BIRTHDAY(i)
;
commit;
exit when c_table%notfound;
end loop;
end;

---------------------------------------------------------
4相關(guān)附助SQL:
select 'type TYPE_' || column_name || ' is table of ' || table_name || '.' ||
column_name || '%type'
from dba_tab_columns
where table_name = 'EMPLOYEES'
and owner = 'HYF'

select 'V_' || column_name || ' TYPE_' || column_name ||';'
from dba_tab_columns
where table_name = 'EMPLOYEES'
and owner = 'HYF'

select 'V_' || column_name || ','
from dba_tab_columns
where table_name = 'EMPLOYEES'
and owner = 'HYF'

select 'V_' || column_name || '(i),'
from dba_tab_columns
where table_name = 'EMPLOYEES'
and owner = 'HYF'
 
 
 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多

    国内精品伊人久久久av高清| 久久精品国产99国产免费| 国产精品香蕉在线的人| 在线精品首页中文字幕亚洲| 日韩欧美国产精品自拍| 免费精品一区二区三区 | 乱女午夜精品一区二区三区| 91后入中出内射在线| 日韩人妻免费视频一专区| 正在播放国产又粗又长| 国产小青蛙全集免费看| 婷婷伊人综合中文字幕| 在线观看日韩欧美综合黄片| 国产女优视频一区二区| 精品少妇人妻一区二区三区| 成人国产激情福利久久| 亚洲一区二区三区三州| 国产精品香蕉一级免费| 欧美乱妇日本乱码特黄大片| 亚洲夫妻性生活免费视频| 国产免费观看一区二区| 99精品人妻少妇一区二区人人妻| 偷自拍亚洲欧美一区二页| 亚洲最新一区二区三区| 日韩精品一区二区亚洲| 狠狠做五月深爱婷婷综合| 日韩精品少妇人妻一区二区| 亚洲精品国产第一区二区多人| 九九蜜桃视频香蕉视频| 这里只有九九热精品视频| 色综合伊人天天综合网中文| 97人摸人人澡人人人超碰| 美女黄色三级深夜福利| 日韩aa一区二区三区| 国产一区日韩二区欧美| 中文字幕欧美视频二区| 亚洲av成人一区二区三区在线| 日韩精品福利在线观看| 国产精品国三级国产专不卡| 欧美日韩综合在线第一页| 国产精品超碰在线观看|