Oracle PL/SQL。选择并进行缩气

发布于 2025-01-19 20:18:52 字数 383 浏览 0 评论 0原文

我想做这样的查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月寒剑心 2025-01-26 20:18:52

当 v_name 为 null 时将 null 与所有书籍匹配或在 v_name 不为 null 时仅匹配一本书(假设 NAME 是唯一的)的一种方法是通过键入更少的字符来实现:

select *
  from BOOK
 where v_name is null
    or NAME = v_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:

select *
  from BOOK
 where v_name is null
    or NAME = v_name

But for commercial apps, I think doing things like this is unwise. It's reasonable to force the user to think.

时光磨忆 2025-01-26 20:18:52

这是您需要的查询结构;

SELECT
    *
FROM
    book 
WHERE (v_name is null) or 
      (v_name is not null and name = v_name);

Here is your needed query structure;

SELECT
    *
FROM
    book 
WHERE (v_name is null) or 
      (v_name is not null and name = v_name);

青萝楚歌 2025-01-26 20:18:52

我认为 NVL 函数可以在这里帮助你 -

v_name VARCHAR2(60); 
SELECT * 
  FROM book 
 WHERE name = NVL(v_name, name);

所以,如果 v_name 为 NULL,列将与自身进行比较并满足你的目的。

I think NVL function can help you here -

v_name VARCHAR2(60); 
SELECT * 
  FROM book 
 WHERE name = NVL(v_name, name);

So, If v_name would be NULL, column would compare with itself and fulfills your purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文