存储过程中如何实现如下的功能:a integer;
b integer;a:=0;
b:=0;select case 字段1 when 1 then
                       a:= a +1
                  when 2 then
                       b:= b +1
       end,
       其他字段
from 表

解决方案 »

  1.   


    decode(字段1,1,a+1,2,b+1)
      

  2.   

    a:= a +1还要把结果赋给a啊
      

  3.   

    --问题描述的不怎么清楚
    decode(字段1,1,a+1,2,b+1)
      

  4.   

    declare
      a       integer;
      b       integer;
      v_字段1 integer;
    begin
      a       := 0;
      b       := 0;
      v_字段1 := 0;  select 字段1 into v_字段1 from 表 where rownum <= 1;  if v_字段1 = 1 then
        a := a + 1;
      else
        if v_字段1 = 2 then
          b := b + 1;
        end if;
      end if;
      dbms_output.put_line('a=' || a);
      dbms_output.put_line('b=' || b);
    end;
      

  5.   

    declare
      a number(18,0);
      b number(18,0);
    begin
      select sum(decode(字段1,1,1,0)),  sum(decode(字段1,2,1,0)) into a, b from 表;
      dbms_output.put_line('字段1中,值为1 的记录行数为:'||to_char(a)||' 值为2 的记录行数为:'||to_char(b));
    end;
    /
      

  6.   


    declare
    a number;
    b number;
    begin
    select count(decode(字段1,1,1)),count(decode(字段1,2,1)) into a,b from tb;
    dbms_output.put_line('字段1是1的记录数目有:'||to_char(a)||'   '||'字段1是2的记录数目有:'||to_char(b));
    end;
      

  7.   

    select count(case when 字段1 = 1 then 1 end) 字段1为1,count(case when 字段1 = 2 then 1 end) 字段1为2 from 表
      

  8.   

    ...非要写一条sql,写俩条sql多简单select count(*) into a from tb where 字段1=1;
    select count(*) into b from tb where 字段1=2;