Oracle uses cursors to delete all record scripts in all user data tables

Source: Internet
Author: User

Application scenario: Because the data in the database involves confidential information, I hope to delete all the data at once, and keep the data table structure for the new project development program.

Test Result: All data is deleted after query

There is a problem: data table If there is a foreign key, the following script may be unsuccessful, please delete or filter out the table yourself, see

How to do this: directly copy the following script contents to Pqsql

--oracle using cursors to delete all record scripts in all user data tables


Declare mystring NVARCHAR2 (1000): = "; --Define the string variables to output

Cursor MyCursor is--defines a cursor

SELECT * FROM User_tables ORDER BY TABLE_NAME; --Query all user tables

Myrecord Mycursor%rowtype; --Define Cursor record type
Counter int: = 0;
Begin
Open mycursor; --Open cursor
If Mycursor%isopen then--Judging open success
Loop-Loop Get recordset
Fetch mycursor into Myrecord; --Get the record in the cursor

If Mycursor%found then--the found property of the cursor determines if there is a record
Begin

mystring:= ' truncate from ' | | Myrecord.table_name;

Dbms_output.put_line (' Current action statement is ' | | mystring);

if (myrecord.table_name<> ' Table_info ') then
Execute immediate ' truncate TABLE ' | | Myrecord.table_name;
End If;

commit;--Immediate Execution

End

Else
Exit
End If;

End Loop;
Else
Dbms_output.put_line (' Cursor not open ');
End If;
Close MyCursor;

End

Oracle uses cursors to delete all record scripts in all user data tables

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.