首页 > 解决方案 > 无法从 pl/sql 中的空值获取 NULL

问题描述

在我的表“Group”中,我没有 name=John,所以我想在“new_name”中获取 NULL 值。

SELECT name INTO new_name FROM Group WHERE name="John";

在 pl/sql 中:

if(new_name IS NULL) then  ---here is the problem,I can't enter in "if",instead of having name=NULL, I have name=" ",and when I try to use if(new_name=" "), there's an error....---
     some code .....
end if;

那么如何检查“if”语句,它是否为NULL(不需要使用EXISTS)?

标签: sqloracleplsql

解决方案


A simple method is to use aggregation:

SELECT MAX(name) INTO new_name
FROM Group 
WHERE name = 'John';

An aggregation query with no GROUP BY is guaranteed to return one row. If all rows are filtered out, then the results of the aggregations are NULL.


推荐阅读