oracle中的null 字段,Oracle下的NULL字段
由于業(yè)務(wù)需求,應(yīng)用需要批量更新表上一個(gè)字段的值,以下是腳本:
SET FEEDBACK ON
SET PAGESIZE 49999
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
v_card_no prepaid_card.card_no%type;
i integer;
cursor card_cur isselect card_no from prepaid_card;
BEGIN
open card_cur;
i := 0;
LOOP
FETCH card_cur INTO v_card_no;
EXIT when card_cur%notfound;
i := i +1;
update prepaid_card set param = '1000000000' where card_no = v_card_no;
IF mod(i, 10000) = 0 THEN
commit;
END IF;
END LOOP;
commit;
dbms_output.put_line('處理總行數(shù)'||i);
close card_cur;
exception
when no_data_found then
dbms_output.put_line('無數(shù)據(jù)');
when others then
dbms_output.put_line('錯(cuò)誤代碼:'||sqlcode||'.'||'錯(cuò)誤描述:'||sqlerrm||'.');
END;
/
exit;
原來的想法是打開游標(biāo)開始查詢,之后根據(jù)fetch得到的記錄進(jìn)行更新,把所有記錄的param字段都設(shè)置為1000000000。
由于查詢打開游標(biāo)時(shí)間過長(zhǎng),更新數(shù)據(jù)時(shí)出現(xiàn)ORA-1555錯(cuò),快照過舊。
在第二次批量更新字段值時(shí),考慮到該批量操作耗時(shí)較長(zhǎng),所以將腳本改成如下內(nèi)容,期望可以避免重復(fù)更新:
SET FEEDBACK ON
SET PAGESIZE 49999
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
v_card_no prepaid_card.card_no%type;
i integer;
cursor card_cur isselect card_no from prepaid_cardwhere param <> '1000000000';
BEGIN
open card_cur;
i := 0;
LOOP
FETCH card_cur INTO v_card_no;
EXIT when card_cur%notfound;
i := i +1;
update prepaid_card set param = '1000000000' where card_no = v_card_no;
IF mod(i, 10000) = 0 THEN
commit;
END IF;
END LOOP;
commit;
dbms_output.put_line('處理總行數(shù)'||i);
close card_cur;
exception
when no_data_found then
dbms_output.put_line('無數(shù)據(jù)');
when others then
dbms_output.put_line('錯(cuò)誤代碼:'||sqlcode||'.'||'錯(cuò)誤描述:'||sqlerrm||'.');
END;
/
exit;
這里問題出現(xiàn)了。param <> '1000000000'其實(shí)不是param = '1000000000'的補(bǔ)集,這里遺漏了一種可能:NULL(空值)。NULL是不參與字段比較的,將sql語句改寫后將會(huì)忽略param字段為NULL類型的記錄,這么做導(dǎo)致了應(yīng)用邏輯處理出現(xiàn)問題,最終進(jìn)行了緊急修復(fù)。
做個(gè)簡(jiǎn)單實(shí)驗(yàn)證明一下:
SQL> create table test (num number,name varchar2(10));
Table created.
SQL> insert into test values(1,'0');
1 row created.
SQL> insert into test values(2,'1');
1 row created.
SQL> insert into test values(3,' ');
1 row created.
SQL>insert into test values(4,null);
1 row created.
SQL>commit;
Commit complete.
SQL> select * from test where name <>'0';
NUM NAME
---------- ----------
2 1
3
SQL>select * from test where name is not null;
NUM NAME
---------- ----------
1 0
2 1
3
SQL> select * from test where name is null;
NUM NAME
---------- ----------
4
SQL> select * from test where name='0';
NUM NAME
---------- ----------
1 0
Conditions Containing Nulls
Condition
Value of A
Evaluation
a IS NULL
10
FALSE
a IS NOT NULL
10
TRUE
a IS NULL
NULL
TRUE
a IS NOT NULL
NULL
FALSE
a = NULL
10
UNKNOWN
a != NULL
10
UNKNOWN
a = NULL
NULL
UNKNOWN
a != NULL
NULL
UNKNOWN
a = 10
NULL
UNKNOWN
a != 10
NULL
UNKNOWN
總結(jié)
以上是生活随笔為你收集整理的oracle中的null 字段,Oracle下的NULL字段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 皇强一九五八私藏酒多少钱一箱一箱多少瓶呢
- 下一篇: php对象怎么拆分字符串数组,在PHP中