我有以下代码:
SELECT cr_ts, msg_id, info
FROM messaging_log
WHERE UPPER(INFO) LIKE '%' || TRIM(UPPER(P_INFO)) || '%'
AND cr_ts >= NVL(P_DATE, SYSDATE-10)
AND msg_id = NVL(P_ID, msg_id)
ORDER BY cr_ts desc;
其中P_INFO,P_DATE和P_ID是参数。在正常情况下,每个参数都是NULL。但是where子句AND msg_id = NVL(P_ID, msg_ID)
不会返回msg_id本身为NULL的行。我错过了什么?
当P_ID和msg_id为NULL时,如何返回所有条目?
答案 0 :(得分:0)
SELECT cr_ts, msg_id, info
FROM messaging_log
WHERE UPPER(INFO) LIKE '%' || TRIM(UPPER(P_INFO)) || '%'
AND cr_ts >= NVL(P_DATE, SYSDATE-10)
AND NVL(msg_id,'*') = NVL(P_ID, '*')
ORDER BY cr_ts desc;
答案 1 :(得分:0)
试试这个;)
SELECT
cr_ts,
msg_id,
info
FROM messaging_log
WHERE UPPER(INFO) LIKE '%' || TRIM(UPPER(P_INFO)) || '%'
AND ((P_DATE IS NULL AND cr_ts >= (SYSDATE - 10)) OR cr_ts >= P_DATE)
AND (P_ID IS NULL OR msg_id = P_ID)
ORDER BY cr_ts DESC;
答案 2 :(得分:-1)
如果msg_id为NULL,则无法使用等于(=)运算符比较NULL值。