Linux ·

Oracle使用游标查询所有数据表备注

功能作用:应用对应的SQL语句,能方便快速的查询Oracle数据库指定用户的所有用户表说明,快速知道每个数据表是做什么的,方便写文档和方案。

运行环境:搭建好Oracle数据库,并使用PQ/SQL Developer软件和指定的数据库账号密码连接上您要查询的数据库。

详细内容如下:

1、使用到的SQL查询脚本如下:

---------------------------------------------------------------------------------------------------------------
--Oracle使用游标查询所有数据表备注

declare 
mytablename NVARCHAR2(200):=''; --定义要查询的数据表名称变量 
mytablecomment NVARCHAR2(200):=''; --定义要查询的数据表注释变量 
commentsql VARCHAR2(2000):=''; --定义要输出的注释结果变量  
cursor mycursor1 is select * from user_tab_comments  order by table_name;--定义游标  
     
myrecord1 mycursor1%rowtype;  --定义游标记录类型  

begin  
open mycursor1;  --打开游标  
if mycursor1%isopen  then  --判断打开成功  
loop --循环获取记录集    
  fetch mycursor1 into myrecord1;
 
  if mycursor1%found then  --游标的found属性判断是否有记录  
  begin
 
    mytablename:=myrecord1.table_name;
    mytablecomment:=myrecord1.comments;
   
    --commentsql:='表名为    '||mytablename||'    注释为    '||mytablecomment;
    commentsql:=mytablename||'    '||mytablecomment;
    dbms_output.put_line(commentsql);    
   
  end;
   
  else
  exit; --获取游标中的记录      
 
  end if;

end loop;  
else    
dbms_output.put_line('游标没有打开');  
end if; 

close mycursor1;

end;

---------------------------------------------------------------------------------------------------------------

2、PL/SQL Developer中的操作一:复制上面的查询脚本到【SQL】选项卡,并执行查询,截图如下:

Oracle使用游标查询所有数据表备注 Linux 第1张

3、PL/SQL Developer中的操作二:切换到【输出】选项卡,查看结果,截图如下:

Oracle使用游标查询所有数据表备注 Linux 第2张

参与评论