开发者

Update local database using remote database data

I got a bit of a problem with a Oracle query

create or replace
PROCEDURE       "LOAD_USERNAME" 
IS
   cursor usu is
      select userid
      from local_user;

BEGIN
   for usu_rec in usu
   loop
      update loc_user set username =(
         selec开发者_如何转开发t cod_user 
            from (
               select cod_user, row_number() over (order by date_createad desc) r
               from user_table@DBLINK where cod_person=usu_rec.userid
            )
            where r = 1
         ) 
      where externaluserid=usu_rec.userid;

   end loop;
END;

Basically, trying to get code of a user(last one created) from other database and update a local table. This seems to work but I takes too much time. I can only check the remote database through a DBLINK.

Please, I want some help for a better way to do this.

I thank your help in advance.


You want to minimise the number of times you go over the network. So you should join to the remote table in your driving cursor and pull the username back there. This will be better as that query is only executed once (indexing/design will determine how well it goes). But your updates will then only be working with local data.

Edit: Removed my PL/SQL as @Aitor's was better


As Sodved said, is better to had the join in your cursor. You can try something like this:

create or replace
PROCEDURE       "LOAD_USERNAME" 
IS
   cursor usu is
select distinct local_user.userid,your_dblink_table.cod_user
      from local_user, user_table@bdlink your_dblink_table
where local_user.userid=your_dblink_table.codperson
and local_user.externaluserid=local_user.userid;
BEGIN
   for usu_rec in usu
   loop
      update loc_user set username =usu_rec.cod_user
where externauserid=usu_rec.userid;
 end loop;
commit;
END;

If you have to load massive updates, you can try a bulk collect/for all approach in the cursor.


Oracle has provided built-in functionality for this sort of thing for several major versions. If you're on an older database you should use replication. In more recent versions this has been deprecated in favour of Streams.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜