我有以下SELECT语句:
SELECT COUNT(*) FROM EPS_PROPOSAL
WHERE NSF_CODE = 14 OR
NSF_CODE = 5 OR
NSF_CODE = 3 OR
NSF_CODE = 45 OR
NSF_CODE = 11 OR
NSF_CODE = 2 OR
NSF_CODE = 7 OR
NSF_CODE = 46 OR
NSF_CODE = 37 OR
NSF_CODE = 22 OR
NSF_CODE = 40 OR
NSF_CODE = 9 OR
NSF_CODE = 47 OR
NSF_CODE = 19 OR
NSF_CODE = 13 OR
NSF_CODE = 4 OR
NSF_CODE = 48 OR
NSF_CODE = 42 OR
NSF_CODE = 49 OR
NSF_CODE = 50 OR
NSF_CODE = 31 OR
NSF_CODE = 27 OR
NSF_CODE = 10 OR
NSF_CODE = 41 OR
NSF_CODE = 39 OR
NSF_CODE = 35 OR
NSF_CODE = 21;
1 COUNT(*) = 902
现在,如果我尝试使用以下UPDATE语句遍历EPS_PROPOSAL表:
UPDATE EPS_PROPOSAL
SET NSF_CODE = (CASE WHEN NSF_CODE = 14 THEN 3
WHEN NSF_CODE = 5 THEN 4
WHEN NSF_CODE = 3 THEN 5
WHEN NSF_CODE = 45 THEN 7
WHEN NSF_CODE = 11 THEN 8
WHEN NSF_CODE = 2 THEN 9
WHEN NSF_CODE = 7 THEN 11
WHEN NSF_CODE = 46 THEN 12
WHEN NSF_CODE = 37 THEN 13
WHEN NSF_CODE = 22 THEN 14
WHEN NSF_CODE = 40 THEN 41
WHEN NSF_CODE = 9 THEN 19
WHEN NSF_CODE = 47 THEN 20
WHEN NSF_CODE = 19 THEN 21
WHEN NSF_CODE = 13 THEN 2
WHEN NSF_CODE = 4 THEN 22
WHEN NSF_CODE = 48 THEN 23
WHEN NSF_CODE = 42 THEN 24
WHEN NSF_CODE = 49 THEN 25
WHEN NSF_CODE = 50 THEN 27
WHEN NSF_CODE = 31 THEN 29
WHEN NSF_CODE = 27 THEN 31
WHEN NSF_CODE = 10 THEN 34
WHEN NSF_CODE = 41 THEN 35
WHEN NSF_CODE = 39 THEN 37
WHEN NSF_CODE = 35 THEN 38
WHEN NSF_CODE = 21 THEN 39 END);
1,866 rows updated.
看来,我的UPDATE语句多次遍历表以完成工作,但是我试图一次遍历表1行,并在满足条件之一的情况下更新每一行-否则,我将覆盖以前更新的内容。
底线-我应该只看到902行已更新。
有什么建议吗?
答案 0 :(得分:3)
问题似乎不是表已被读取多次。您的UPDATE
语句没有WHERE
子句,因此无论nsf_code
的值如何,它都会更新表中的每一行。如果要更新行的子集,则需要包括一个WHERE
子句
UPDATE eps_proposal
SET nsf_code = (CASE ...)
WHERE nsf_code IN (14,5,3,...,21)