下面這個和我的很相似,不用自己寫了,就用他的吧!
最近做了乙個需求,需要批量更新資料庫
表,但是因為涉及到的資料較多(千萬條),如果直接用sql更新,估計會把pl/sql弄垮
sql如下:update online_product set online_flag = '0' where status = 'on'
所以,寫了乙個儲存過程,以備忘:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[sql]view plain
copy
?
declare
cursor product_id_list is
select product_id from online_product where status = 'on';
commit_count number := 0;
total_count number := 0;
begin
for pid in product_id_list loop
total_count := total_count + 1;
commit_count := commit_count + 1;
update online_product setonline_flag = '0'
where status = 'on'
and product_id = pid.product_id;
if commit_count > 1000 then
commit;
commit_count := 0;
end if;
end loop;
commit;
dbms_output.put_line('total count:' || total_count);
end;
declare
cursor product_id_list is
select product_id from online_product where status = 'on';
commit_count number := 0;
total_count number := 0;
begin
for pid in product_id_list loop
total_count := total_count + 1;
commit_count := commit_count + 1;
update online_product setonline_flag = '0'
where status = 'on'
and product_id = pid.product_id;
if commit_count > 1000 then
commit;
commit_count := 0;
end if;
end loop;
commit;
dbms_output.put_line('total count:' || total_count);
end;
以 mod 判斷提交更為簡便(每500條提交一次)。
[sql]view plain
copy
print?
declare
v_count number := 0;
cursor cur_r_meas_basic_info is
select trade_flow_no
from r_meas_basic_info i
where stake_no='1110102010002418'
and trad_time<(select to_date('2016-05-19','yyyy-mm-dd') from dual);
begin
for i in cur_r_meas_basic_info loop
update r_trade_rec t set t.batch_no ='111'
where t.trade_flow_no =i.trade_flow_no and t.batch_no is
null;
v_count:= v_count+1;
if mod (v_count,500) = 0 then
commit;
end if;
end loop;
commit;
end;
儲存過程批量更新
批量更新 mysql更新語句很簡單,更新一條資料的某個字段,一般這樣寫 複製 如下 update mytable set myfield value where other field other value 如果更新同一欄位為同乙個值,mysql也很簡單,修改下where即可 複製 如下 upda...
mysql 儲存過程批量更新
最近做乙個批量更新的操作,由於是臨時需求,就想著在資料庫直接操作,不在 裡動手了,結合網上的一些資料,做如下處理 1.先建立乙個臨時表,匯入需要變動的資料 drop table if exists t barcode create table t barcode barcode varchar 32...
MYSQL批量建表儲存過程
分表比較多的情況,如何批量建立,可通過儲存過程實現 建立乙個儲存過程 紅色表結構,藍色為表名及表數量 delimiter create procedure sp create tab begin set str id int 11 not null auto increment comment 自增...