测试结果:经查询已删除所有数据
存在问题:数据表如果存在外键的话下面脚本可能执行不成功,请自行删除或者过滤掉该表,见下图
操作办法:直接将下面的脚本内容复制到PQSQL中执行即可
?
--Oracle使用游标删除所有用户数据表中的所有记录脚本
declare mystring NVARCHAR2(1000):=''; --定义要输出的字符串变量?
cursor mycursor is --定义游标?
select * from user_tables order by table_name; --查询所有用户表
? ? ?
myrecord mycursor%rowtype;? --定义游标记录类型?
Counter int :=0;?
begin?
open mycursor;? --打开游标?
if mycursor%isopen? then? --判断打开成功?
loop --循环获取记录集? ?
fetch mycursor into myrecord; --获取游标中的记录? ? ? ?
if mycursor%found then? --游标的found属性判断是否有记录?
begin
?
mystring:='truncate from '||myrecord.table_name;
dbms_output.put_line('当前操作语句为'||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('游标没有打开');?
end if;?
close mycursor;
end;