Example of Oracle cursor, recursive query, subquery, and batch update

Source: Internet
Author: User

-- Create a temporary table
Create Table tj_org_new
(
Deptcode nvarchar2 (255 ),
Orgdesc nvarchar2 (255 ),
Organtype nvarchar2 (255 ),
Levelfact number (6 ),
Parentdeptcode nvarchar2 (255)
)
Tablespace Test
Pctfree 10
Initrans 1
Maxtrans 255
Storage
(
Initial 64 K
Minextents 1
Maxextents Unlimited
);
-- Initialize temporary table data
Insert into tj_org_new (deptcode, orgdesc, organtype, levelfact, parentdeptcode) Select deptcode, "org", organtype, level, parentdeptcode from "tj_org"
Connect by prior deptcode = parentdeptcode
Start with parentdeptcode = 1 order by level;

/*
Batch logical processing of data using cursors
*/
Declare
Varuserdeptcode varchar (255); -- defines the same type as the table Field
Cursor mycursor is -- defines the cursor
Select userdeptcode from tj_user;

My_record mycursor % rowtype; -- defines the cursor record type
Counter INT: = 0;

Begin
Open mycursor; -- open the cursor
If mycursor % isopen then -- determines whether the open is successful
Loop -- Obtain Record Sets cyclically
Fetch mycursor into my_record; -- get records in the cursor
If mycursor % found then -- the cursor's found attribute determines whether a record exists
-- Perform actual business processing begin
If my_record.userdeptcode = 90033751 then -- provincial-level user updates
Update tj_user set userorgcode = 90033751 where userdeptcode = 90033751;
Dbms_output.put_line (my_record.userdeptcode | 'A ');
Else -- Non-provincial users updated
Update tj_user set userorgcode =
(Select deptcode from (select deptcode, orgdesc, organtype, parentdeptcode, levelfact from tj_org_new
Connect by prior parentdeptcode = deptcode
Start with deptcode = my_record.userdeptcode
Order by levelfact) Where organtype = 1 and levelfact = 2)
Where (userdeptcode <> 90033751) and
(Userdeptcode in (select deptcode from tj_org_new
Connect by prior parentdeptcode = deptcode
Start with deptcode = my_record.userdeptcode ));
Dbms_output.put_line (my_record.userdeptcode | 'B ');
End if;
-- Actual business processing end
Else
Exit;
End if;
End loop;
Else
Dbms_output.put_line ('cursor not opened ');
End if;
Close mycursor;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.