Home » SQL & PL/SQL » SQL & PL/SQL » Update query for 2 lakhs rows
Update query for 2 lakhs rows [message #587146] Wed, 12 June 2013 13:42 Go to next message
satyamhcl
Messages: 3
Registered: December 2009
Junior Member
I have 2 tables with 3 common columns (Col1, col2,Primary). One common column name is primary (oracle key word).Around 2 lakhs rows to be updated and No indexes are used on these tables. I need to write an update query as shown below.
Emp table
Col1 col2 primay
100 101 y
103 104 n
201 105 y
100 101 y

Dept table
Col1 col2 primay
100 101 null
103 104 null
000 656 null

Update query Result
Col1 col2 primary
100 101 y
103 104 n
Please help me.
Thanks


Re: Update query for 2 lakhs rows [message #587147 is a reply to message #587146] Wed, 12 June 2013 13:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Update query for 2 lakhs rows [message #587148 is a reply to message #587146] Wed, 12 June 2013 13:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68773
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You update what from what?
And what do you mean by "update" the final result has less rows than the 2 tables you gave.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Before, Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.

Regards
Michel

[Updated on: Wed, 12 June 2013 13:44]

Report message to a moderator

Re: Update query for 2 lakhs rows [message #587160 is a reply to message #587146] Wed, 12 June 2013 15:19 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
By whatever I could understand from the vague information provided by you, I could do this much:-

EMP TABLE
create table emp as select * from(
with data as
(
select 100 col1, 101 col2, 'y' primary from dual
union all
select 103 col1, 104 col2, 'n' primary from dual
union all
select 201 col1, 105 col2, 'y' primary from dual
union all
select 100 col1, 101 col2, 'y' primary from dual
) 
select * from data
)
)


DEPT TABLE
create table dept as select * from emp where 1=2;
insert into dept values(100,101,null);
insert into dept values(103,104,null);
insert into dept values(000,656,null);
commit;


select * from emp;
      COL1       COL2 PRIMARY
---------- ---------- -------
       100        101 y      
       103        104 n      
       201        105 y      
       100        101 y      

4 rows selected.


select * from dept;
      COL1       COL2 PRIMARY
---------- ---------- -------
       100        101       
       103        104       
         0        656        

3 rows selected.  


So, you want to update column primary of table dept from table emp based on col1 and col2 matching conditions.

Here you go:-
declare
   type typ_emp is table of emp%rowtype;
   var_emp typ_Emp;
cursor c1 is select * from emp;
begin
   open c1;
   loop
      fetch c1 bulk collect into var_emp;
      exit when c1%notfound;
   end loop;
   for i in 1..var_emp.count
   loop
      update dept set primary=var_emp(i).primary
      where dept.col1=var_emp(i).col1
      and dept.col2=var_emp(i).col2;
     
      commit;
   end loop;
   close c1;
exception 
   when others then 
   dbms_output.put_line('came into exception');
end;
/


select * from dept;
      COL1       COL2 PRIMARY
---------- ---------- -------
       100        101 y      
       103        104 n      
         0        656        

3 rows selected.

[Updated on: Wed, 12 June 2013 15:21]

Report message to a moderator

Re: Update query for 2 lakhs rows [message #587366 is a reply to message #587160] Fri, 14 June 2013 08:39 Go to previous messageGo to next message
_jum
Messages: 577
Registered: February 2008
Senior Member
Here my feedback:
1) for performance you should better use FORALL
2) some LOOPS are not necessary
3) why do You COMMIT in the LOOP ?
4) exception when others then
IS A BUG
Here my version:
DECLARE

   TYPE typ_emp IS TABLE OF emp%ROWTYPE;
   var_emp typ_emp;

CURSOR c1 IS
SELECT *
 FROM emp;

BEGIN
 
  OPEN c1;
 
  FETCH c1 BULK COLLECT INTO var_emp;

  FORALL i IN 1..var_emp.count
  UPDATE dept
     SET primary=var_emp(i).primary
   WHERE dept.col1=var_emp(i).col1
     AND dept.col2=var_emp(i).col2;
        
   CLOSE c1;

END;
/

SELECT *
  FROM dept;

      COL1       COL2 PRIMARY
---------- ---------- -------
       100        101 y      
       103        104 n      
         0        656        

3 rows selected.

Re: Update query for 2 lakhs rows [message #587376 is a reply to message #587366] Fri, 14 June 2013 09:03 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Or, you could of course use a correlated update instead of all that PL/SQL
Re: Update query for 2 lakhs rows [message #587378 is a reply to message #587376] Fri, 14 June 2013 09:05 Go to previous messageGo to next message
_jum
Messages: 577
Registered: February 2008
Senior Member
of course, this is much better Laughing
Re: Update query for 2 lakhs rows [message #587408 is a reply to message #587366] Fri, 14 June 2013 10:46 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Well, even I could have used FORALL, however, OP has not mentioned the Oracle version.

Secondly, I just gave a code skeleton for OP to try and learn by himself, and not the perfect answer(spoon feeding) Razz
Re: Update query for 2 lakhs rows [message #587409 is a reply to message #587376] Fri, 14 June 2013 10:48 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
pablolee wrote on Fri, 14 June 2013 19:33
Or, you could of course use a correlated update instead of all that PL/SQL


With PL/SQL features(BULK COLLECT etc.), it would be faster and cost effective. Don't you agree?
Re: Update query for 2 lakhs rows [message #587411 is a reply to message #587366] Fri, 14 June 2013 10:50 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@_jum

You would like to use SQL%BULK_EXCEPTIONS in your code, don't you?
Re: Update query for 2 lakhs rows [message #587412 is a reply to message #587409] Fri, 14 June 2013 10:53 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>With PL/SQL features(BULK COLLECT etc.), it would be faster and cost effective. Don't you agree?
No, I do not agree.
PL/SQL can NEVER be faster than plain SQL.
Only SQL interacts with the data within the DB.
PL/SQL only adds overhead & context switches into processing the data, since PL/SQL must utilize SQL to obtain or change data.
Re: Update query for 2 lakhs rows [message #587416 is a reply to message #587366] Fri, 14 June 2013 11:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68773
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
_jum wrote on Fri, 14 June 2013 15:39
Here my feedback:...
4) exception when others then
IS A BUG
...


When you think that Lalit Kumar B gave a lesson to another poster in another topic on this point, I feel really disheartened to ever find a good code somewhere someday.

Regards
Michel

[Updated on: Fri, 14 June 2013 11:24]

Report message to a moderator

Re: Update query for 2 lakhs rows [message #587431 is a reply to message #587409] Fri, 14 June 2013 14:06 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Lalit Kumar B wrote on Fri, 14 June 2013 16:48
pablolee wrote on Fri, 14 June 2013 19:33
Or, you could of course use a correlated update instead of all that PL/SQL


With PL/SQL features(BULK COLLECT etc.), it would be faster and cost effective. Don't you agree?

No, i don't. at all. As has already been said, Properly constructed SQL will always be faster than PL/SQL doing the same job. You need to start paying a little more attention to what you're doing. The problem here is that you have some shockingly bad habits, because you seem to want to learn just enough to be dangerous. Your code contains two of the very worst practices in PL/SQL coding, there is no excuse for that, it's just bad. Now, don't take offense here, your not being picked on, but it must be made clear to other posters and lurkers that the code that you posted is a very long way away from being an optimal solution to the posted problem.

[Updated on: Fri, 14 June 2013 14:15]

Report message to a moderator

Re: Update query for 2 lakhs rows [message #587432 is a reply to message #587416] Fri, 14 June 2013 14:30 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Fri, 14 June 2013 21:54
I feel really disheartened to ever find a good code somewhere someday.

No issues Sir, if you keep correcting me every time, I would definitely be better than just good. Don't be disheartened at all. I am still, and always be - A LEARNER!
Re: Update query for 2 lakhs rows [message #587437 is a reply to message #587408] Fri, 14 June 2013 15:17 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Lalit Kumar B
Well, even I could have used FORALL, however, OP has not mentioned the Oracle version.

FORALL was, unless I'm wrong, introduced in Oracle 8i which was released in 1998. Does the OP use Oracle 7 (or 8), where FORALL doesn't work? Possible, but not probable.

[EDIT: disabled smilies]

[Updated on: Fri, 14 June 2013 15:18]

Report message to a moderator

Re: Update query for 2 lakhs rows [message #587444 is a reply to message #587437] Fri, 14 June 2013 15:42 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Littlefoot wrote on Sat, 15 June 2013 01:47

FORALL was, unless I'm wrong, introduced in Oracle 8i which was released in 1998. Does the OP use Oracle 7 (or Cool, where FORALL doesn't work? Possible, but not probable.


If you see the code which _jum has posted:-
FORALL i IN 1..var_emp.count
  UPDATE dept
     SET primary=var_emp(i).primary
   WHERE dept.col1=var_emp(i).col1
     AND dept.col2=var_emp(i).col2;


It is referencing the individual elements of a collection within the SET and WHERE clauses of a DML statement in a FORALL construct. Which is only possible in 11g. Prior versions would throw PLS-00436 error.
Re: Update query for 2 lakhs rows [message #587458 is a reply to message #587444] Sat, 15 June 2013 01:02 Go to previous message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

Yes that will throw the error.So for earlier version the code can be done like this.I have tested in Oracle 10g.

DECLARE

   type typ_primary is table of emp.primary%type index by binary_integer;
   type typ_col1 is table of emp.col1%type index by binary_integer;
   type typ_col2 is table of emp.col2%type index by binary_integer;
   var_primary typ_primary;
   var_col1 typ_col1;
   var_col2 typ_col2;

cursor c1 is
select primary,col1,col2
 FROM emp;

BEGIN
 
  open c1;
  loop 
  fetch c1 bulk collect into var_primary,var_col1,var_col2 LIMIT 1;--may be required incase of large records 
  exit when var_primary.count=0;
  forall i in 1..var_primary.count
  UPDATE dept
     set primary=var_primary(i)
   where dept.col1=var_col1(i)
     and dept.col2=var_col2(i);
        end loop;        
   CLOSE c1;

END;
Previous Topic: Query help
Next Topic: Stored Procdure with multiple table join with aggregate functions.
Goto Forum:
  


Current Time: Tue Dec 30 00:04:18 CST 2025