利用場景:由于數(shù)據(jù)庫中的數(shù)據(jù)觸及機密信息,希望1次性能刪除掉所有數(shù)據(jù),只保存數(shù)據(jù)表結(jié)構(gòu),供新項目開發(fā)程序用
測試結(jié)果:經(jīng)查詢已刪除所有數(shù)據(jù)
存在問題:數(shù)據(jù)表如果存在外鍵的話下面腳本可能履行不成功,請自行刪除或過濾掉該表,見下圖
操作辦法:直接將下面的腳本內(nèi)容復(fù)制到PQSQL中履行便可
--Oracle使用游標(biāo)刪除所有用戶數(shù)據(jù)表中的所有記錄腳本
declare mystring NVARCHAR2(1000):=''; --定義要輸出的字符串變量
cursor mycursor is --定義游標(biāo)
select * from user_tables order by table_name; --查詢所有用戶表
myrecord mycursor%rowtype; --定義游標(biāo)記錄類型
Counter int :=0;
begin
open mycursor; --打開游標(biāo)
if mycursor%isopen then --判斷打開成功
loop --循環(huán)獲得記錄集
fetch mycursor into myrecord; --獲得游標(biāo)中的記錄
if mycursor%found then --游標(biāo)的found屬性判斷是不是有記錄
begin
mystring:='truncate from '||myrecord.table_name;
dbms_output.put_line('當(dāng)前操作語句為'||mystring);
if(myrecord.table_name<>'TABLE_INFO') then
execute immediate 'truncate table '||myrecord.table_name;
end if;
commit;--立即履行
end;
else
exit;
end if;
end loop;
else
dbms_output.put_line('游標(biāo)沒有打開');
end if;
close mycursor;
end;
下一篇 分布式計算之并行計算