表a:字段a_id 、a_company
表b:字段b_id 、b_values
字段都是varchar2类型,oracle 9i
目的:从表a中取出a_company的值,做一个判断,然后根据判断结果在表b中插入相应的数据。CREATE OR REPLACE Procedure aaa as
  x_company  varchar2(12);
  x_id varchar2(6);
  cursor csor_aaa is select t.a_id,t.a_company from 表a t where t.a_company like '001%';
begin
  open csor_aaa;
  fetch csor_aaa into x_id,x_company;
  while csor_company%found loop
  
     if substr(x_company,7,2)=10 then
         insert into 表b y (b_id,b_values) values ('&x_id','000001');
     elseif substr(x_company,7,2)>10 and substr(x_company,7,2)<30 then   
         insert into 表b y (b_id,b_values) values ('&x_id','000002');
     elseif substr(x_company,7,2)=30 then   
         insert into 表b (b_id,b_values) values ('&x_id','000003');         
     end if    
        
     fetch csor_aaa into x_id,x_company;
  end loop;
  close csor_aaa;
  commit;
end aaa;

解决方案 »

  1.   

    substr(x_company,7,2)='30' or substr(x_company,7,2)='40'可以不可以写成  substr(x_company,7,2) in ('30','40')
      

  2.   

    可以那么写。楼主用的oracle哪个版本。如果9以上,可以直接用case来实现。
      

  3.   

    是9i case可以提高性能吗?
      

  4.   

    用case一个语句就可以写好了,也不用游标了
      

  5.   

    用case 怎么一个语句就好啊?帮忙提示一下啊?
      

  6.   

    insert into 表b y (b_id,b_values)
    select t.a_id,
    case when substr(t.a_company,7,2) =10 then '000001'
    when substr(t.a_company,7,2)>10 and substr(t.a_company,7,2)<30 then  '000002'
    when substr(t.a_company,7,2)=30 then '000003' end 
     from 表a t where t.a_company like '001%';
      

  7.   

    like '001%';也可以用变量的
    我看到你前面的帖子好像是想用变量的
      

  8.   

    回复 :冰可能是我没说明白,上边的那个是简单写的,实际上在每个 elseif 中有好多判断(and 和 or 形式的),根据判断然后不一定要在 表b中插入几条记录。上边的那个case不知道可以满足吗?谢谢
      

  9.   

    case when then
    when then
    ...
    end
    也可以判断很多阿
    我不太明白你的意思
      

  10.   

    如果你觉得case复杂
    就用游标好了你也可以两种方法都用一下
    正好可以比较一下
    那个效率更高就用哪个了
      

  11.   

    能简单说下,不用case用游标的方法吗?谢谢
      

  12.   

    不好意思,我第一次用这个东西。这是其中一小部分 if 。CREATE OR REPLACE Procedure User_JueSe as
      x_company  varchar2(12);
      x_user_id char(6);
      x_user_zwjb char(2);
      x_1 char(2);
      x_2 char(2);
      x_description varchar2(80);
      cursor csor_company is select t.user_id,t.company,t.zwjb,t.description from ry_jbxx t where t.company like '3601%';
    begin
      open csor_company;
      fetch csor_company into x_user_id,x_company,x_user_zwjb,x_description;
      while csor_company%found loop
      
         x_1:=substr(x_company,7,2);
         x_2:=substr(x_company,9,2);
         x_description:=trim(description);
         
         if x_1='00' then  
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000008');
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000405');               
         ----------------------------------------------------------------------------        
         elseif to_number(x_1)>'50' and zwjb='40' then                 
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000004');      
         elseif to_number(x_1)>'50' and x_2='00' and zwjb='50'  then   
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000003');
         elseif to_number(x_1)>'50' and x_2='01' and zwjb='50'  then   
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000050');
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000002');
         elseif to_number(x_1)>'50' and x_2='02' and zwjb='50'  then   
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000050');
             insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000001');
                                     
         end if;       fetch csor_rybh_hh into x_user_id,x_company,x_user_zwjb,x_description;
        
      end loop;
      close csor_company;
      commit;
    end User_JueSe;
      

  13.   

    有什么好的方式优化一下吗?用上边说的case可以实现吗?
      

  14.   

    9i 的存促过程 里面用汉字判断可以吗?比如:
     if x_description='电脑' then
      

  15.   

    可以declare aa varchar2(200);
    begin
    aa:='电脑';
    if aa='电脑' then
    dbms_output.put_line('电脑' );
    end if;
    end;