在oracle中使用游标插入和更新记录 [英] INSERT and UPDATE a record using cursors in oracle

查看:104
本文介绍了在oracle中使用游标插入和更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表-studentstudLoad都具有2个字段studIDstudName.我想将数据从student表加载到stuLoad表中. 如果studLoad表中已经存在数据,则应对其进行更新,否则应将其插入.以下是我这样做的代码:

I have 2 tables- student and studLoad both having 2 fields studID and studName. I want to load data from student table into stuLoad table. If the data already exists in the studLoad table, then it should be updated else it should be inserted. following is my code to do so:

    create or replace procedure studentLoad is
v_id student.studID%type;
v_name student.studName%type;
v_sn studLoad.studName%type;
cursor cur_load is
select * from student;


begin
  open cur_load;
  loop
   fetch cur_load into v_id,v_name;

   exit when cur_load%notfound;
   select studName into v_sn from studLoad where studID = v_id;
   if(v_sn!= v_name) then
      update studLoad set studName= v_name where studID= v_id;
   else
      insert into studLoad values(v_id,v_name);
   dbms_output.put_line(v_id || ' ' || v_name);
   end if;
  end loop;
      close cur_load;
end;

它不起作用. studLoad表中的行不会更新.我该如何解决?在SQL Server中,我们使用IF EXISTS(select...from stuLoad..)来检查表中是否存在记录,是否可以在Oracle中执行相同的操作?如果是的话,请告知我.

It's not working. the rows in studLoad table are noT updated. How do I solve this? In SQL server we use IF EXISTS(select...from stuLoad..) to check if the record exists in the table, is there a way to do the same in Oracle? if yes then please let me know the same.

推荐答案

这是一种效率很低的方法.您可以使用 merge 语句,然后就不需要游标,循环或(如果可以的话)PL /SQL.

This is a highly inefficient way of doing it. You can use the merge statement and then there's no need for cursors, looping or (if you can do without) PL/SQL.

MERGE INTO studLoad l
USING ( SELECT studId, studName FROM student ) s
ON (l.studId = s.studId)
WHEN MATCHED THEN
  UPDATE SET l.studName = s.studName
   WHERE l.studName != s.studName
WHEN NOT MATCHED THEN 
INSERT (l.studID, l.studName)
VALUES (s.studId, s.studName)

请确保您完成commit,以便能够在数据库中看到它.

Make sure you commit, once completed, in order to be able to see this in the database.

要真正回答您的问题,我将执行以下操作.这样的好处是可以完成大部分SQL工作,并且仅基于rowid(表中的唯一地址)进行更新.

To actually answer your question I would do it something like as follows. This has the benefit of doing most of the work in SQL and only updating based on the rowid, a unique address in the table.

它声明一个类型,您可以一次将数据批量放入10,000行中.然后分别处理这些行.

It declares a type, which you place the data within in bulk, 10,000 rows at a time. Then processes these rows individually.

但是,正如我所说的那样,它的效率不如merge.

However, as I say this will not be as efficient as merge.

declare

   cursor c_data is
    select b.rowid as rid, a.studId, a.studName
      from student a
      left outer join studLoad b
        on a.studId = b.studId
       and a.studName <> b.studName
           ;

   type t__data is table of c_data%rowtype index by binary_integer;
   t_data t__data;

begin

   open c_data;
   loop
      fetch c_data bulk collect into t_data limit 10000;

      exit when t_data.count = 0;

      for idx in t_data.first .. t_data.last loop
         if t_data(idx).rid is null then
            insert into studLoad (studId, studName)
            values (t_data(idx).studId, t_data(idx).studName);
         else
            update studLoad
               set studName = t_data(idx).studName
             where rowid = t_data(idx).rid
                   ;
         end if;
      end loop;

   end loop;
   close c_data;

end;
/

这篇关于在oracle中使用游标插入和更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆