PL SQL - 类似于 (!a?0:a) 功能

发布于 2024-08-07 20:54:45 字数 1189 浏览 1 评论 0原文

我在游标中有一个简单的查询

Cursor some_cursor IS
    select 
       sum(some_field) 
    from some_table table_1 
    where 
        table_1.TYPE =1                         
        AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
        AND table_1.f2 = 962
        AND table_1.f3 = 41813; 

然后我做

   fetch some_cursor into some_var;--some_var is of type Number, cursor is open

当我运行此查询时, some_var 有可能为 NULL。在这种情况下,id喜欢取0值;

类似

    --C like pseudocode of what I want
   (some_cursor!=Null?(fetch some_cursor into some_var):(some_var:=0))

有没有办法做到这一点?我正在考虑将上面的查询重写为

Cursor some_cursor IS
    select 
       sum(some_field),count(*) 
    from some_table table_1 
    where 
        table_1.TYPE =1                         
        AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
        AND table_1.f2 = 962
        AND table_1.f3 = 41813; 

然后写入,

   fetch some_cursor into some_var,some_counter;

   if (some_counter = 0) then
   begin
       some_var :=0;
   end

但这意味着重写 10 个游标(是的,不是那么多)。也许 plsql 有更干净的方法。

提前致谢

I have a simple query in a cursor

Cursor some_cursor IS
    select 
       sum(some_field) 
    from some_table table_1 
    where 
        table_1.TYPE =1                         
        AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
        AND table_1.f2 = 962
        AND table_1.f3 = 41813; 

Then i do

   fetch some_cursor into some_var;--some_var is of type Number, cursor is open

When I run this query, there's the chance that some_var will be NULL. In that case, id like it to take the 0 value;

Something like

    --C like pseudocode of what I want
   (some_cursor!=Null?(fetch some_cursor into some_var):(some_var:=0))

Is there a way to do this? I was thinking of rewriting the above query to

Cursor some_cursor IS
    select 
       sum(some_field),count(*) 
    from some_table table_1 
    where 
        table_1.TYPE =1                         
        AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
        AND table_1.f2 = 962
        AND table_1.f3 = 41813; 

and then writing

   fetch some_cursor into some_var,some_counter;

   if (some_counter = 0) then
   begin
       some_var :=0;
   end

but this implies rewriting 10 cursors (yes, not so many). Maybe plsql has a cleaner way.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天气好吗我好吗 2024-08-14 20:54:45

尝试:

SELECT NVL(SUM(some_field),0)
  from some_table table_1 
 where table_1.TYPE =1                                                 
   AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
   AND table_1.f2 = 962
   AND table_1.f3 = 41813;

Try:

SELECT NVL(SUM(some_field),0)
  from some_table table_1 
 where table_1.TYPE =1                                                 
   AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
   AND table_1.f2 = 962
   AND table_1.f3 = 41813;
無心 2024-08-14 20:54:45

您可能会在那里更正其他几个问题:

  • 使用隐式游标而不是显式游标
  • 如果可以避免,请勿将函数应用于表列。

因此:

Declare
   some_var Number;
Begin
   select Coalesce(sum(some_field),0)
   into   some_var
   from   some_table table_1   
   where  table_1.TYPE =  1                                                   
    AND   table_1.date >= date '2009-09-05' 
    AND   table_1.date <  date '2009-09-06' 
    AND   table_1.f2   =  962  
    AND   table_1.f3   =  41813;
End;

You might correct a couple of other issues while you're there:

  • Use implicit cursors instead of explicit
  • Don't apply a function to a table column if you can avoid it.

thus:

Declare
   some_var Number;
Begin
   select Coalesce(sum(some_field),0)
   into   some_var
   from   some_table table_1   
   where  table_1.TYPE =  1                                                   
    AND   table_1.date >= date '2009-09-05' 
    AND   table_1.date <  date '2009-09-06' 
    AND   table_1.f2   =  962  
    AND   table_1.f3   =  41813;
End;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文