Oracle PL/SQL。选择并进行缩气
我想做这样的查询:
v_name VARCHAR2(60); SELECT * FROM book WHERE name = v_name
但是如果“v_name”为 NULL,则程序必须是:
SELECT * FROM book
我不想在 WHERE 中设置过滤器健康)状况。如果 v_name 为 NULL,我不想获取 name=NULL 的所有记录,而是获取所有记录。
我试过这个:
SELECT * FROM book WHERE name (CASE WHEN v_name IS NULL THEN name ELSE v_name END);
但这不起作用。
谢谢。
I want to do a Query like this:
v_name VARCHAR2(60); SELECT * FROM book WHERE name = v_name
But if "v_name" is NULL, the program must be:
SELECT * FROM book
I don't wan't to set the filter in the WHERE condition. If v_name is NULL i dont want to get all the recodrs with name=NULL, but ALL the records.
I have tried this:
SELECT * FROM book WHERE name (CASE WHEN v_name IS NULL THEN name ELSE v_name END);
But it doesn't work.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当 v_name 为 null 时将 null 与所有书籍匹配或在 v_name 不为 null 时仅匹配一本书(假设 NAME 是唯一的)的一种方法是通过键入更少的字符来实现:
但对于商业应用程序,我认为这样做是不明智的。强迫用户思考是合理的。
One way to match null with all books when v_name is null or match only a book (assuming NAME is unique) when v_name is not null is to do it by typing fewer characters:
But for commercial apps, I think doing things like this is unwise. It's reasonable to force the user to think.
这是您需要的查询结构;
Here is your needed query structure;
我认为 NVL 函数可以在这里帮助你 -
所以,如果 v_name 为 NULL,列将与自身进行比较并满足你的目的。
I think NVL function can help you here -
So, If v_name would be NULL, column would compare with itself and fulfills your purpose.