【问题标题】:PL/SQL check if query returns emptyPL/SQL 检查查询是否返回空
【发布时间】:2011-08-22 17:43:55
【问题描述】:

我正在编写一个程序,我需要检查我的选择查询是否返回了空记录。 (本例中是否有x,y架子)

我该怎么做?

我试过这个:

temp shelves.loadability%TYPE := NULL;
BEGIN

select loadability into temp from shelves where rownumber = x and columnnumber = y;
IF temp IS NOT NULL THEN
/* do something when it's not empty */
ELSE
/* do the other thing when it's empty */
END IF;

但是 if 的第二个分支永远不会工作......

编辑:

哦,原来如此简单……

temp shelves.loadability%TYPE;
BEGIN

select count(*) into temp from shelves where rownumber = x and columnnumber = y;
IF temp != 0 THEN
/* do something when it's not empty */
ELSE
/* do the other thing when it's empty */
END IF;

END;

【问题讨论】:

  • select count(*) 也不是检查行的好方法。请参阅下面的答案以获得更优化的方法
  • 您的编辑应该是您问题的答案。

标签: plsql


【解决方案1】:

catch first not Wanted 条件并使用 count(1),因为 count(*) 实际上试图计算一些东西并添加 rownum=1,你只需要一个第一个不匹配条件。我用这个说法。

       declare
      v_check number;
    begin
      select count(1) into v_check 
from table
 where condition(something not wanted) AND rownum=1;

      if v_check = 0 then 
           --do something else
      elsif v_check = 1 --dont want theat
         rise some error or more..
      end if;
    end;

只为你

select count(1) into v_check from dual where exists (select count(1) 
    from table
     where condition AND rownum=1);

if v_check = 0 then --nothing found
                 something...
          elsif v_check = 1 --found something
            something...
          end if;
        end;

【讨论】:

    【解决方案2】:

    异常处理也是我首先想到的,但如果您不想让自己负担处理所有不同情况的负担,我倾向于使用select count(*) from。 count(*) 的好处是它总是返回一些东西(假设你的查询是合法的)。在这种情况下,您可以计算它是否返回 0(不匹配)或更多(在这种情况下,您可以做一些事情。

    你可以得到这样的东西:

    declare
      v_count number := 0;
    begin
      select count(*) into v_count from table where condition;
    
      if v_count = 0 then
          --do something
      else
          --do something else
      end if;
    end;
    

    【讨论】:

      【解决方案3】:

      使用异常处理程序

      Begin
        select column
        into variable
        from table
        where ...;
      
        -- Do something with your variable
      
      exception
       when no_data_found then
          -- Your query returned no rows --
      
       when too_many_rows
          -- Your query returned more than 1 row --
      
      end;
      

      【讨论】:

      • 这是好的做法吗?您正在使用 exception,例如 condition
      • 不,这不是好的做法,请参阅错误捕获部分中的“提示”:postgresql.org/docs/current/static/…
      • 这叫异常处理。这就是 plsql 的工作方式。
      • 我认为这不是一个好方法。因为,根据定义exceptional condition 是程序不知道如何处理的情况。在这种情况下,最安全的做法是优雅地终止进程/函数/过程。在良好的编程实践方面,在 graceful shutdown routine 中实施业务规则并不是一个好主意!
      • 当您在 PLSQL 中执行 select ... into 时,您必须期望 0、1 或更多结果。你需要处理这些情况。当然,您可以先执行 select count(*),但不能 100% 保证您稍后执行 select into 时数据没有更改。处理 no_data_found 和 too_many_rows 异常是使用 select .. 进入 PLSQL 查询的一种非常标准的方式。
      【解决方案4】:

      只为存在的记录执行工作通常更像 SQL。换句话说,您可以为每次匹配执行您的任务,如果没有匹配,您就不要执行此操作。所以你甚至不需要 IF-ELSE 构造。

      我不建议使用游标来完成这项工作,因为这与我的第一个建议相反,即您应该更像 SQL 那样做。但是如果你必须这样做,那么光标可能会做你想做的事情。

      是的,我知道这并不能直接回答您的问题。

      【讨论】:

      • 但是我必须做一个任务,如果没有这样的架子……我必须用新架子插入一个元组。抱歉,如果不清楚。
      • 那么,我该怎么做?
      • @WonderCsabo:抱歉,我没有及时回复问题。很高兴你解决了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2013-10-12
      • 1970-01-01
      相关资源
      最近更新 更多