oracle存储过程中if语句

1.完整的存储过程如下:

这个存储过程内容可能有点多,这是我平时的项目用到的存储过程,刚好里面用到了if语句,所以我就拿来了给大家看一下

create or replace procedure p_censor_stat_status_org_real(r_codes       out varchar2,
                                                          r_msg         out varchar2,
                                                          r_data        out varchar2,
                                                          r_cur_data    out sys_refcursor,
                                                          info007_start in varchar2,
                                                          info007_end   in varchar2,
                                                          info008_start in varchar2,
                                                          info008_end   in varchar2,
                                                          p_info010     in varchar2,
                                                          p_info012     in varchar2,
                                                          p_info011     in varchar2,
                                                          p_info013     in varchar2,
                                                          p_info003     in varchar2,
                                                          p_info004     in varchar2,
                                                          p_info101     in varchar2) is

  --统计年度督办状态信息个数(单位)

  m_uuid         varchar2(32); -- 唯一id
  m_time         varchar2(30); -- 执行时间
  m_info101      varchar2(30); -- 执行时间
  m_info101_word varchar2(30); -- 执行时间

begin
  r_codes := 'success';
  r_msg   := null;
  r_data  := null;
  --生成唯一id信息
  select sys_guid() into m_uuid from dual;
  dbms_output.put_line(m_uuid);

  -- 记录插入时间
  select to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss')
    into m_time
    from dual;

  if p_info101 = '0' then
    m_info101_word := '推进中';
  elsif p_info101 = '1' then
    m_info101_word := '已销号';
  elsif p_info101 = '2' then
    m_info101_word := '已取消';
  
  end if;

  begin
  
    if p_info101 is not null then
      m_info101 := p_info101;
      insert into t_tmp_censor_stat_status_org
        (id, uuid, count, value, name, updatetime, indexs)
        (select sys_guid(),
                m_uuid,
                nvl(count(*), 0),
                '0',
                m_info101_word,
                m_time,
                1
           from t_censor_flow_inst_major t
          where (t.del_flag = '0' or t.del_flag is null)
            and t.info101 = m_info101
            and (info007_start is null or t.info007 > info007_start)
            and (info007_end is null or t.info007 < info007_end)
            and (info008_start is null or t.info008 > info008_start)
            and (info008_end is null or t.info008 > info008_end)
            and (p_info010 is null or instr(t.info010, p_info010) > 0)
            and (p_info011 is null or instr(t.info011, p_info011) > 0)
            and (p_info012 is null or instr(t.info012, p_info012) > 0)
            and (p_info013 is null or instr(t.info013, p_info013) > 0)
            and (p_info003 is null or instr(t.info003, p_info003) > 0)
            and (p_info004 is null or t.info004 = p_info004));
    
    else
      -- 正常办理:当前正在推进中的,没有出现过延期/取消
      insert into t_tmp_censor_stat_status_org
        (id, uuid, count, value, name, updatetime, indexs)
        (select sys_guid(),
                m_uuid,
                nvl(count(*), 0),
                '0',
                '正常办理',
                m_time,
                1
           from t_censor_flow_inst_major t
          where (t.del_flag = '0' or t.del_flag is null)
            and t.info102 = '0'
            and t.info101 = '0'
            and (info007_start is null or t.info007 > info007_start)
            and (info007_end is null or t.info007 < info007_end)
            and (info008_start is null or t.info008 > info008_start)
            and (info008_end is null or t.info008 > info008_end)
            and (p_info010 is null or instr(t.info010, p_info010) > 0)
            and (p_info011 is null or instr(t.info011, p_info011) > 0)
            and (p_info012 is null or instr(t.info012, p_info012) > 0)
            and (p_info013 is null or instr(t.info013, p_info013) > 0)
            and (p_info003 is null or instr(t.info003, p_info003) > 0)
            and (p_info004 is null or t.info004 = p_info004));
    
      -- 延期办理:出现延期
      insert into t_tmp_censor_stat_status_org
        (id, uuid, count, value, name, updatetime, indexs)
        (select sys_guid(),
                m_uuid,
                nvl(count(*), 0),
                '1',
                '延期办理',
                m_time,
                3
           from t_censor_flow_inst_major t
          where (t.del_flag = '0' or t.del_flag is null)
            and t.info102 = '1'
            and (info007_start is null or t.info007 > info007_start)
            and (info007_end is null or t.info007 < info007_end)
            and (info008_start is null or t.info008 > info008_start)
            and (info008_end is null or t.info008 > info008_end)
            and (p_info010 is null or instr(t.info010, p_info010) > 0)
            and (p_info011 is null or instr(t.info011, p_info011) > 0)
            and (p_info012 is null or instr(t.info012, p_info012) > 0)
            and (p_info013 is null or instr(t.info013, p_info013) > 0)
            and (p_info003 is null or instr(t.info003, p_info003) > 0)
            and (p_info004 is null or t.info004 = p_info004));
    
      -- 办理销号:当前状态是办理销号的
      insert into t_tmp_censor_stat_status_org
        (id, uuid, count, value, name, updatetime, indexs)
        (select sys_guid(),
                m_uuid,
                nvl(count(*), 0),
                '3',
                '办结销号',
                m_time,
                2
           from t_censor_flow_inst_major t
          where (t.del_flag = '0' or t.del_flag is null)
            and t.info101 = '1'
            and (info007_start is null or t.info007 > info007_start)
            and (info007_end is null or t.info007 < info007_end)
            and (info008_start is null or t.info008 > info008_start)
            and (info008_end is null or t.info008 > info008_end)
            and (p_info010 is null or instr(t.info010, p_info010) > 0)
            and (p_info011 is null or instr(t.info011, p_info011) > 0)
            and (p_info012 is null or instr(t.info012, p_info012) > 0)
            and (p_info013 is null or instr(t.info013, p_info013) > 0)
            and (p_info003 is null or instr(t.info003, p_info003) > 0)
            and (p_info004 is null or t.info004 = p_info004));
    
      -- 已取消:当前状态是已取消的
      insert into t_tmp_censor_stat_status_org
        (id, uuid, count, value, name, updatetime, indexs)
        (select sys_guid(),
                m_uuid,
                nvl(count(*), 0),
                '4',
                '已取消',
                m_time,
                4
           from t_censor_flow_inst_major t
          where (t.del_flag = '0' or t.del_flag is null)
            and t.info101 = '2'
            and (info007_start is null or t.info007 > info007_start)
            and (info007_end is null or t.info007 < info007_end)
            and (info008_start is null or t.info008 > info008_start)
            and (info008_end is null or t.info008 > info008_end)
            and (p_info010 is null or instr(t.info010, p_info010) > 0)
            and (p_info011 is null or instr(t.info011, p_info011) > 0)
            and (p_info012 is null or instr(t.info012, p_info012) > 0)
            and (p_info013 is null or instr(t.info013, p_info013) > 0)
            and (p_info003 is null or instr(t.info003, p_info003) > 0)
            and (p_info004 is null or t.info004 = p_info004));
    end if;
  
  end;
  begin
    open r_cur_data for
      select t.name, t.count
        from t_tmp_censor_stat_status_org t
       where t.uuid = m_uuid
       order by t.indexs;
  end;
  /*清除临时表数据*/
  delete t_tmp_censor_stat_status_org t where t.uuid = m_uuid;
  commit;

end p_censor_stat_status_org_real;

2.需要注意的地方

在写的时候,如果有多种情况,一定不要使用else if,因为这种用法在oracle存储过程中是错误的,应该使用elsif,第一次使用的时候我就用错了,所以怎么调都会报错,最后才发现是这里出了问题。所以正确的用法是下面这样的:

  if p_info101 = '0' then
    m_info101_word := '推进中';
  elsif p_info101 = '1' then
    m_info101_word := '已销号';
  elsif p_info101 = '2' then
    m_info101_word := '已取消';
  
  end if;

还有一点,存储过程中的判断是否相等使用=,给变量赋值的时候使用:=

3.结语

好了,这边文章就到这里了,上边所写的东西都是我平时测试好的,大家有什么其他疑问的话可以在评论里提出来,大家一起探讨,一起进步。

你可能感兴趣的:(存储过程)