| Update query for 2 lakhs rows [message #587146] |
Wed, 12 June 2013 13:42  |
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 #587148 is a reply to message #587146] |
Wed, 12 June 2013 13:44   |
 |
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   |
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   |
_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 #587409 is a reply to message #587376] |
Fri, 14 June 2013 10:48   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
pablolee wrote on Fri, 14 June 2013 19:33Or, 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 #587416 is a reply to message #587366] |
Fri, 14 June 2013 11:24   |
 |
Michel Cadot
Messages: 68773 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
_jum wrote on Fri, 14 June 2013 15:39Here 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   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Lalit Kumar B wrote on Fri, 14 June 2013 16:48pablolee wrote on Fri, 14 June 2013 19:33Or, 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   |
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:54I 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   |
 |
Littlefoot
Messages: 21826 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Lalit Kumar BWell, 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
|
|
|
|
|
|
|
|