lorcx 2016-04-10 02:12 采纳率: 0%
浏览 1474

oracle存储过程使用游标的问题

create or replace procedure test3(
templete_id in varchar2
) as
t_id_tmp varchar2(50);
ty_id_tmp varchar2(50);
cursor type_cur is select t1.id as type_id,t1.type_name from t_type t1,t_templete_link t2,t_templete t3
where t1.id = t2.type_id and t2.templete_id = t3.id and t3.id = templete_id;
-- n number;
begin
select sys_guid() into t_id_tmp from dual;

 --复制模板信息
 insert into t_templete
 select t_id_tmp as id, t.templete_name
   from t_templete t
  where t.id = templete_id;
 commit;      

   dbms_output.put_line(templete_id); 
 --复制类别信息
-- declare 

 begin
   for type_rec in type_cur
     loop
        -- n:=n+1;
        -- dbms_output.put_line(1);        
         select sys_guid() into ty_id_tmp from dual;       
         insert into t_type
           select ty_id_tmp as id, type_rec.type_name from dual;
            -- from t_type t2
          --  where t2.id = type_rec.type_id;
         --  commit;   

          insert into t_templete_link   
              select sys_guid() as id,t_id_tmp as TEMPLETE_ID ,ty_id_tmp as type_id from dual; -- t_templete_link t2
              --where t2.templete_id = templete_id;
          commit;    
       end loop;
   end;

--dbms_output.put_line(t_id_tmp);

exception
  when others then
    Rollback;

end test3;

如果我传入的参数templete_id=1,我的t_cur循环的时候循环了3次。
但如果游标中的变变量templete_id 替换成 ‘1’就循环2次 这才是正常结果为什么

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-05 09:25
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在你的SQL程序中,当你尝试将templete_id替换为'1'时,你实际上是在尝试从数据库查询中获取第一条记录。这会导致游标执行两次循环,因为每次循环都会更新templete_id变量,并且在第一次循环之后会再次调用for type_rec in type_cur来获取新的数据。

    为了正确地处理这种情况,你应该在循环内部更新templete_id值,而不是在外部。这样,每次循环都将得到一个新的templete_id值,并且在循环结束后将这个值传递给下一个循环。

    以下是修改后的代码:

    create or replace procedure test3(
        template_id in varchar2
    ) as
    begin
        -- 初始化templete_id
        temp_template_id := template_id;
    
        -- 获取所有类型ID
        for type_rec in (select id from t_type where id = temp_template_id)
          loop
            -- 更新templete_id
            temp_template_id := type_rec.id;
            -- 执行循环体
            for type_rec in (select * from t_type where id = type_rec.id)
              loop
                -- 执行其他操作
                -- ...
              end loop;
          end loop;
    end test3;
    

    在这个版本的代码中,我们首先初始化temp_template_id为传入的template_id,然后通过一个内层循环遍历所有的类型ID。对于每个类型的ID,我们都执行外层循环以获取所有相关的信息。这样,无论template_id如何变化,每次循环都只会执行一次,从而避免了不必要的循环。

    评论

报告相同问题?