Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Oracle Cursor help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-04, 19:40
da_coolestofall da_coolestofall is offline
Registered User
 
Join Date: Mar 2004
Location: California
Posts: 58
Oracle Cursor help

Is it possible to have a cursor pulling data from 2 different tables and marking it as FOR UPDATE and then using the UPDATE statement for one table with the WHERE CURRENT OF clause. For example can I do this

CURSOR C2 IS
SELECT t1.date_created, t2.customer, t2.name1, t2.sman
FROM lead_when t1, leads t2
where t1.leadno = t2.leadno AND
t1.lastsale is null
FOR UPDATE of t2.sman;
BEGIN
FOR c2_index in c2
LOOP
IF(c2_index.date_created < sysdate -180)
THEN
INSERT into sman_null_backup (sman, customer, name1) VALUES (c2_index.sman, c2_index.customer, c2_index.name1);
UPDATE leads
SET sman = NULL
WHERE CURRENT OF C2;
END IF;
END LOOP;
CLOSE C2;
COMMIT;

Will this update the LEADS table. Also, do I need to have commit there, or is it implicit?
Reply With Quote
  #2 (permalink)  
Old 09-30-04, 07:27
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Yes, you can do that. You should explicitly commit.

Your code can be improved. First, the condition " IF(c2_index.date_created < sysdate -180)" should be part of the cursor definition. Second, there is no need to close the for-loop cursor explicitly (in fact, that will fail). So:
Code:
DECLARE CURSOR C2 IS SELECT t1.date_created, t2.customer, t2.name1, t2.sman FROM lead_when t1, leads t2 WHERE t1.leadno = t2.leadno AND t1.lastsale is null AND t1.date_created < sysdate-180 FOR UPDATE of t2.sman; BEGIN FOR c2_index in c2 LOOP INSERT into sman_null_backup (sman, customer, name1) VALUES (c2_index.sman, c2_index.customer, c2_index.name1); UPDATE leads SET sman = NULL WHERE CURRENT OF C2; END LOOP; COMMIT; END;
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 09-30-04, 13:47
da_coolestofall da_coolestofall is offline
Registered User
 
Join Date: Mar 2004
Location: California
Posts: 58
Thanks! And about the condition being part of the cursor....I kinda feel silly for not doing that. Thank you so much for pointing that out.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On