oracle中的null 字段,Oracle下的NULL字段
由于業務需求,應用需要批量更新表上一個字段的值,以下是腳本:
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('處理總行數'||i);
close card_cur;
exception
when no_data_found then
dbms_output.put_line('無數據');
when others then
dbms_output.put_line('錯誤代碼:'||sqlcode||'.'||'錯誤描述:'||sqlerrm||'.');
END;
/
exit;
原來的想法是打開游標開始查詢,之后根據fetch得到的記錄進行更新,把所有記錄的param字段都設置為1000000000。
由于查詢打開游標時間過長,更新數據時出現ORA-1555錯,快照過舊。
在第二次批量更新字段值時,考慮到該批量操作耗時較長,所以將腳本改成如下內容,期望可以避免重復更新:
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('處理總行數'||i);
close card_cur;
exception
when no_data_found then
dbms_output.put_line('無數據');
when others then
dbms_output.put_line('錯誤代碼:'||sqlcode||'.'||'錯誤描述:'||sqlerrm||'.');
END;
/
exit;
這里問題出現了。param <> '1000000000'其實不是param = '1000000000'的補集,這里遺漏了一種可能:NULL(空值)。NULL是不參與字段比較的,將sql語句改寫后將會忽略param字段為NULL類型的記錄,這么做導致了應用邏輯處理出現問題,最終進行了緊急修復。
做個簡單實驗證明一下:
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
總結
以上是生活随笔為你收集整理的oracle中的null 字段,Oracle下的NULL字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 皇强一九五八私藏酒多少钱一箱一箱多少瓶呢
- 下一篇: c语言输入6名学生5门课程的成绩,求解输