Oracle uses a cursor to delete all record scripts in all user data tables. oracle cursor

Source: Internet
Author: User
Tags oracle cursor

Oracle uses a cursor to delete all record scripts in all user data tables. oracle cursor

Application Scenario: because the data in the database involves confidential information, we hope to delete all the data at a time and only keep the data table structure for the development of new projects.

Test Result: All data has been deleted.

Problem: if the data table has a foreign key, the following script may fail to be executed. delete or filter the table by yourself. See

Operation Method: copy the following script content to PQSQL for execution.

 

-- Oracle deletes all record scripts in all user data tables with a cursor


Declare mystring NVARCHAR2 (1000): = ''; -- Define the string variable to be output

Cursor mycursor is -- defines the cursor

Select * from user_tables order by table_name; -- Query all user tables

Myrecord 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 myrecord; -- get records in the cursor

If mycursor % found then -- the cursor's found attribute determines whether a record exists
Begin

Mystring: = 'truncate from' | myrecord. table_name;

Dbms_output.put_line ('current operation statement is '| mystring );

If (myrecord. table_name <> 'table _ info') then
Execute immediate 'truncate table' | myrecord. table_name;
End if;

Commit; -- run now

End;

Else
Exit;
End if;

End loop;
Else
Dbms_output.put_line ('cursor not opened ');
End if;
Close mycursor;

End;

 

Related Article

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.