在做項目時,經常需要在量表之間進行字段賦值進行初始化
例如:
update TB_PER_INTERNALDISMISSION ti
set ti.C_UNITID =
nvl((select ts.C_OLDUNITID
from TB_PER_STAFFRECORD ts
where ts.c_empoid = ti.c_empoid
and ts.c_recordtype = '7'
and ts.c_recordtime =
(select max(staff.c_recordtime)
from TB_PER_STAFFRECORD staff
where staff.c_empoid = ts.c_empoid
and staff.c_recordtype = '7')
),0)
我們可以看到,在上面的代碼充分估計到了空值的現象(nvl),並對可能的多條記錄只取記錄最新的列表,但依然可能報錯
這裏就需要仔細查看自己的代碼邏輯,一般來說是自己的代碼邏輯出現了問題,如果確定沒有問題,則可能是數據庫裏面存在重複數據
select count(1),ts.c_empoid,ts.c_recordtime
from TB_PER_STAFFRECORD ts
where ts.c_recordtype = '7'
and ts.c_recordtime =
(select max(staff.c_recordtime)
from TB_PER_STAFFRECORD staff
where staff.c_empoid = ts.c_empoid
and staff.c_recordtype = '7')
group by ts.c_empoid,ts.c_recordtime
having count(1)>1
這就是對上面代碼進行重複數據的查詢,從而找到了重複數據